Daily bump.
[gcc.git] / gcc / modulo-sched.c
1 /* Swing Modulo Scheduling implementation.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Ayal Zaks and Mustafa Hagog <zaks,mustafa@il.ibm.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "diagnostic-core.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "hard-reg-set.h"
31 #include "regs.h"
32 #include "function.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "insn-attr.h"
36 #include "except.h"
37 #include "recog.h"
38 #include "sched-int.h"
39 #include "target.h"
40 #include "cfgloop.h"
41 #include "expr.h"
42 #include "params.h"
43 #include "gcov-io.h"
44 #include "ddg.h"
45 #include "tree-pass.h"
46 #include "dbgcnt.h"
47 #include "df.h"
48
49 #ifdef INSN_SCHEDULING
50
51 /* This file contains the implementation of the Swing Modulo Scheduler,
52 described in the following references:
53 [1] J. Llosa, A. Gonzalez, E. Ayguade, M. Valero., and J. Eckhardt.
54 Lifetime--sensitive modulo scheduling in a production environment.
55 IEEE Trans. on Comps., 50(3), March 2001
56 [2] J. Llosa, A. Gonzalez, E. Ayguade, and M. Valero.
57 Swing Modulo Scheduling: A Lifetime Sensitive Approach.
58 PACT '96 , pages 80-87, October 1996 (Boston - Massachusetts - USA).
59
60 The basic structure is:
61 1. Build a data-dependence graph (DDG) for each loop.
62 2. Use the DDG to order the insns of a loop (not in topological order
63 necessarily, but rather) trying to place each insn after all its
64 predecessors _or_ after all its successors.
65 3. Compute MII: a lower bound on the number of cycles to schedule the loop.
66 4. Use the ordering to perform list-scheduling of the loop:
67 1. Set II = MII. We will try to schedule the loop within II cycles.
68 2. Try to schedule the insns one by one according to the ordering.
69 For each insn compute an interval of cycles by considering already-
70 scheduled preds and succs (and associated latencies); try to place
71 the insn in the cycles of this window checking for potential
72 resource conflicts (using the DFA interface).
73 Note: this is different from the cycle-scheduling of schedule_insns;
74 here the insns are not scheduled monotonically top-down (nor bottom-
75 up).
76 3. If failed in scheduling all insns - bump II++ and try again, unless
77 II reaches an upper bound MaxII, in which case report failure.
78 5. If we succeeded in scheduling the loop within II cycles, we now
79 generate prolog and epilog, decrease the counter of the loop, and
80 perform modulo variable expansion for live ranges that span more than
81 II cycles (i.e. use register copies to prevent a def from overwriting
82 itself before reaching the use).
83
84 SMS works with countable loops (1) whose control part can be easily
85 decoupled from the rest of the loop and (2) whose loop count can
86 be easily adjusted. This is because we peel a constant number of
87 iterations into a prologue and epilogue for which we want to avoid
88 emitting the control part, and a kernel which is to iterate that
89 constant number of iterations less than the original loop. So the
90 control part should be a set of insns clearly identified and having
91 its own iv, not otherwise used in the loop (at-least for now), which
92 initializes a register before the loop to the number of iterations.
93 Currently SMS relies on the do-loop pattern to recognize such loops,
94 where (1) the control part comprises of all insns defining and/or
95 using a certain 'count' register and (2) the loop count can be
96 adjusted by modifying this register prior to the loop.
97 TODO: Rely on cfgloop analysis instead. */
98 \f
99 /* This page defines partial-schedule structures and functions for
100 modulo scheduling. */
101
102 typedef struct partial_schedule *partial_schedule_ptr;
103 typedef struct ps_insn *ps_insn_ptr;
104
105 /* The minimum (absolute) cycle that a node of ps was scheduled in. */
106 #define PS_MIN_CYCLE(ps) (((partial_schedule_ptr)(ps))->min_cycle)
107
108 /* The maximum (absolute) cycle that a node of ps was scheduled in. */
109 #define PS_MAX_CYCLE(ps) (((partial_schedule_ptr)(ps))->max_cycle)
110
111 /* Perform signed modulo, always returning a non-negative value. */
112 #define SMODULO(x,y) ((x) % (y) < 0 ? ((x) % (y) + (y)) : (x) % (y))
113
114 /* The number of different iterations the nodes in ps span, assuming
115 the stage boundaries are placed efficiently. */
116 #define CALC_STAGE_COUNT(max_cycle,min_cycle,ii) ((max_cycle - min_cycle \
117 + 1 + ii - 1) / ii)
118 /* The stage count of ps. */
119 #define PS_STAGE_COUNT(ps) (((partial_schedule_ptr)(ps))->stage_count)
120
121 /* A single instruction in the partial schedule. */
122 struct ps_insn
123 {
124 /* Identifies the instruction to be scheduled. Values smaller than
125 the ddg's num_nodes refer directly to ddg nodes. A value of
126 X - num_nodes refers to register move X. */
127 int id;
128
129 /* The (absolute) cycle in which the PS instruction is scheduled.
130 Same as SCHED_TIME (node). */
131 int cycle;
132
133 /* The next/prev PS_INSN in the same row. */
134 ps_insn_ptr next_in_row,
135 prev_in_row;
136
137 };
138
139 /* Information about a register move that has been added to a partial
140 schedule. */
141 struct ps_reg_move_info
142 {
143 /* The source of the move is defined by the ps_insn with id DEF.
144 The destination is used by the ps_insns with the ids in USES. */
145 int def;
146 sbitmap uses;
147
148 /* The original form of USES' instructions used OLD_REG, but they
149 should now use NEW_REG. */
150 rtx old_reg;
151 rtx new_reg;
152
153 /* The number of consecutive stages that the move occupies. */
154 int num_consecutive_stages;
155
156 /* An instruction that sets NEW_REG to the correct value. The first
157 move associated with DEF will have an rhs of OLD_REG; later moves
158 use the result of the previous move. */
159 rtx insn;
160 };
161
162 typedef struct ps_reg_move_info ps_reg_move_info;
163
164 /* Holds the partial schedule as an array of II rows. Each entry of the
165 array points to a linked list of PS_INSNs, which represents the
166 instructions that are scheduled for that row. */
167 struct partial_schedule
168 {
169 int ii; /* Number of rows in the partial schedule. */
170 int history; /* Threshold for conflict checking using DFA. */
171
172 /* rows[i] points to linked list of insns scheduled in row i (0<=i<ii). */
173 ps_insn_ptr *rows;
174
175 /* All the moves added for this partial schedule. Index X has
176 a ps_insn id of X + g->num_nodes. */
177 vec<ps_reg_move_info> reg_moves;
178
179 /* rows_length[i] holds the number of instructions in the row.
180 It is used only (as an optimization) to back off quickly from
181 trying to schedule a node in a full row; that is, to avoid running
182 through futile DFA state transitions. */
183 int *rows_length;
184
185 /* The earliest absolute cycle of an insn in the partial schedule. */
186 int min_cycle;
187
188 /* The latest absolute cycle of an insn in the partial schedule. */
189 int max_cycle;
190
191 ddg_ptr g; /* The DDG of the insns in the partial schedule. */
192
193 int stage_count; /* The stage count of the partial schedule. */
194 };
195
196
197 static partial_schedule_ptr create_partial_schedule (int ii, ddg_ptr, int history);
198 static void free_partial_schedule (partial_schedule_ptr);
199 static void reset_partial_schedule (partial_schedule_ptr, int new_ii);
200 void print_partial_schedule (partial_schedule_ptr, FILE *);
201 static void verify_partial_schedule (partial_schedule_ptr, sbitmap);
202 static ps_insn_ptr ps_add_node_check_conflicts (partial_schedule_ptr,
203 int, int, sbitmap, sbitmap);
204 static void rotate_partial_schedule (partial_schedule_ptr, int);
205 void set_row_column_for_ps (partial_schedule_ptr);
206 static void ps_insert_empty_row (partial_schedule_ptr, int, sbitmap);
207 static int compute_split_row (sbitmap, int, int, int, ddg_node_ptr);
208
209 \f
210 /* This page defines constants and structures for the modulo scheduling
211 driver. */
212
213 static int sms_order_nodes (ddg_ptr, int, int *, int *);
214 static void set_node_sched_params (ddg_ptr);
215 static partial_schedule_ptr sms_schedule_by_order (ddg_ptr, int, int, int *);
216 static void permute_partial_schedule (partial_schedule_ptr, rtx);
217 static void generate_prolog_epilog (partial_schedule_ptr, struct loop *,
218 rtx, rtx);
219 static int calculate_stage_count (partial_schedule_ptr, int);
220 static void calculate_must_precede_follow (ddg_node_ptr, int, int,
221 int, int, sbitmap, sbitmap, sbitmap);
222 static int get_sched_window (partial_schedule_ptr, ddg_node_ptr,
223 sbitmap, int, int *, int *, int *);
224 static bool try_scheduling_node_in_cycle (partial_schedule_ptr, int, int,
225 sbitmap, int *, sbitmap, sbitmap);
226 static void remove_node_from_ps (partial_schedule_ptr, ps_insn_ptr);
227
228 #define NODE_ASAP(node) ((node)->aux.count)
229
230 #define SCHED_PARAMS(x) (&node_sched_param_vec[x])
231 #define SCHED_TIME(x) (SCHED_PARAMS (x)->time)
232 #define SCHED_ROW(x) (SCHED_PARAMS (x)->row)
233 #define SCHED_STAGE(x) (SCHED_PARAMS (x)->stage)
234 #define SCHED_COLUMN(x) (SCHED_PARAMS (x)->column)
235
236 /* The scheduling parameters held for each node. */
237 typedef struct node_sched_params
238 {
239 int time; /* The absolute scheduling cycle. */
240
241 int row; /* Holds time % ii. */
242 int stage; /* Holds time / ii. */
243
244 /* The column of a node inside the ps. If nodes u, v are on the same row,
245 u will precede v if column (u) < column (v). */
246 int column;
247 } *node_sched_params_ptr;
248
249 typedef struct node_sched_params node_sched_params;
250 \f
251 /* The following three functions are copied from the current scheduler
252 code in order to use sched_analyze() for computing the dependencies.
253 They are used when initializing the sched_info structure. */
254 static const char *
255 sms_print_insn (const_rtx insn, int aligned ATTRIBUTE_UNUSED)
256 {
257 static char tmp[80];
258
259 sprintf (tmp, "i%4d", INSN_UID (insn));
260 return tmp;
261 }
262
263 static void
264 compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
265 regset used ATTRIBUTE_UNUSED)
266 {
267 }
268
269 static struct common_sched_info_def sms_common_sched_info;
270
271 static struct sched_deps_info_def sms_sched_deps_info =
272 {
273 compute_jump_reg_dependencies,
274 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
275 NULL,
276 0, 0, 0
277 };
278
279 static struct haifa_sched_info sms_sched_info =
280 {
281 NULL,
282 NULL,
283 NULL,
284 NULL,
285 NULL,
286 sms_print_insn,
287 NULL,
288 NULL, /* insn_finishes_block_p */
289 NULL, NULL,
290 NULL, NULL,
291 0, 0,
292
293 NULL, NULL, NULL, NULL,
294 NULL, NULL,
295 0
296 };
297
298 /* Partial schedule instruction ID in PS is a register move. Return
299 information about it. */
300 static struct ps_reg_move_info *
301 ps_reg_move (partial_schedule_ptr ps, int id)
302 {
303 gcc_checking_assert (id >= ps->g->num_nodes);
304 return &ps->reg_moves[id - ps->g->num_nodes];
305 }
306
307 /* Return the rtl instruction that is being scheduled by partial schedule
308 instruction ID, which belongs to schedule PS. */
309 static rtx
310 ps_rtl_insn (partial_schedule_ptr ps, int id)
311 {
312 if (id < ps->g->num_nodes)
313 return ps->g->nodes[id].insn;
314 else
315 return ps_reg_move (ps, id)->insn;
316 }
317
318 /* Partial schedule instruction ID, which belongs to PS, occurred in
319 the original (unscheduled) loop. Return the first instruction
320 in the loop that was associated with ps_rtl_insn (PS, ID).
321 If the instruction had some notes before it, this is the first
322 of those notes. */
323 static rtx
324 ps_first_note (partial_schedule_ptr ps, int id)
325 {
326 gcc_assert (id < ps->g->num_nodes);
327 return ps->g->nodes[id].first_note;
328 }
329
330 /* Return the number of consecutive stages that are occupied by
331 partial schedule instruction ID in PS. */
332 static int
333 ps_num_consecutive_stages (partial_schedule_ptr ps, int id)
334 {
335 if (id < ps->g->num_nodes)
336 return 1;
337 else
338 return ps_reg_move (ps, id)->num_consecutive_stages;
339 }
340
341 /* Given HEAD and TAIL which are the first and last insns in a loop;
342 return the register which controls the loop. Return zero if it has
343 more than one occurrence in the loop besides the control part or the
344 do-loop pattern is not of the form we expect. */
345 static rtx
346 doloop_register_get (rtx head ATTRIBUTE_UNUSED, rtx tail ATTRIBUTE_UNUSED)
347 {
348 #ifdef HAVE_doloop_end
349 rtx reg, condition, insn, first_insn_not_to_check;
350
351 if (!JUMP_P (tail))
352 return NULL_RTX;
353
354 /* TODO: Free SMS's dependence on doloop_condition_get. */
355 condition = doloop_condition_get (tail);
356 if (! condition)
357 return NULL_RTX;
358
359 if (REG_P (XEXP (condition, 0)))
360 reg = XEXP (condition, 0);
361 else if (GET_CODE (XEXP (condition, 0)) == PLUS
362 && REG_P (XEXP (XEXP (condition, 0), 0)))
363 reg = XEXP (XEXP (condition, 0), 0);
364 else
365 gcc_unreachable ();
366
367 /* Check that the COUNT_REG has no other occurrences in the loop
368 until the decrement. We assume the control part consists of
369 either a single (parallel) branch-on-count or a (non-parallel)
370 branch immediately preceded by a single (decrement) insn. */
371 first_insn_not_to_check = (GET_CODE (PATTERN (tail)) == PARALLEL ? tail
372 : prev_nondebug_insn (tail));
373
374 for (insn = head; insn != first_insn_not_to_check; insn = NEXT_INSN (insn))
375 if (!DEBUG_INSN_P (insn) && reg_mentioned_p (reg, insn))
376 {
377 if (dump_file)
378 {
379 fprintf (dump_file, "SMS count_reg found ");
380 print_rtl_single (dump_file, reg);
381 fprintf (dump_file, " outside control in insn:\n");
382 print_rtl_single (dump_file, insn);
383 }
384
385 return NULL_RTX;
386 }
387
388 return reg;
389 #else
390 return NULL_RTX;
391 #endif
392 }
393
394 /* Check if COUNT_REG is set to a constant in the PRE_HEADER block, so
395 that the number of iterations is a compile-time constant. If so,
396 return the rtx that sets COUNT_REG to a constant, and set COUNT to
397 this constant. Otherwise return 0. */
398 static rtx
399 const_iteration_count (rtx count_reg, basic_block pre_header,
400 HOST_WIDEST_INT * count)
401 {
402 rtx insn;
403 rtx head, tail;
404
405 if (! pre_header)
406 return NULL_RTX;
407
408 get_ebb_head_tail (pre_header, pre_header, &head, &tail);
409
410 for (insn = tail; insn != PREV_INSN (head); insn = PREV_INSN (insn))
411 if (NONDEBUG_INSN_P (insn) && single_set (insn) &&
412 rtx_equal_p (count_reg, SET_DEST (single_set (insn))))
413 {
414 rtx pat = single_set (insn);
415
416 if (CONST_INT_P (SET_SRC (pat)))
417 {
418 *count = INTVAL (SET_SRC (pat));
419 return insn;
420 }
421
422 return NULL_RTX;
423 }
424
425 return NULL_RTX;
426 }
427
428 /* A very simple resource-based lower bound on the initiation interval.
429 ??? Improve the accuracy of this bound by considering the
430 utilization of various units. */
431 static int
432 res_MII (ddg_ptr g)
433 {
434 if (targetm.sched.sms_res_mii)
435 return targetm.sched.sms_res_mii (g);
436
437 return ((g->num_nodes - g->num_debug) / issue_rate);
438 }
439
440
441 /* A vector that contains the sched data for each ps_insn. */
442 static vec<node_sched_params> node_sched_param_vec;
443
444 /* Allocate sched_params for each node and initialize it. */
445 static void
446 set_node_sched_params (ddg_ptr g)
447 {
448 node_sched_param_vec.truncate (0);
449 node_sched_param_vec.safe_grow_cleared (g->num_nodes);
450 }
451
452 /* Make sure that node_sched_param_vec has an entry for every move in PS. */
453 static void
454 extend_node_sched_params (partial_schedule_ptr ps)
455 {
456 node_sched_param_vec.safe_grow_cleared (ps->g->num_nodes
457 + ps->reg_moves.length ());
458 }
459
460 /* Update the sched_params (time, row and stage) for node U using the II,
461 the CYCLE of U and MIN_CYCLE.
462 We're not simply taking the following
463 SCHED_STAGE (u) = CALC_STAGE_COUNT (SCHED_TIME (u), min_cycle, ii);
464 because the stages may not be aligned on cycle 0. */
465 static void
466 update_node_sched_params (int u, int ii, int cycle, int min_cycle)
467 {
468 int sc_until_cycle_zero;
469 int stage;
470
471 SCHED_TIME (u) = cycle;
472 SCHED_ROW (u) = SMODULO (cycle, ii);
473
474 /* The calculation of stage count is done adding the number
475 of stages before cycle zero and after cycle zero. */
476 sc_until_cycle_zero = CALC_STAGE_COUNT (-1, min_cycle, ii);
477
478 if (SCHED_TIME (u) < 0)
479 {
480 stage = CALC_STAGE_COUNT (-1, SCHED_TIME (u), ii);
481 SCHED_STAGE (u) = sc_until_cycle_zero - stage;
482 }
483 else
484 {
485 stage = CALC_STAGE_COUNT (SCHED_TIME (u), 0, ii);
486 SCHED_STAGE (u) = sc_until_cycle_zero + stage - 1;
487 }
488 }
489
490 static void
491 print_node_sched_params (FILE *file, int num_nodes, partial_schedule_ptr ps)
492 {
493 int i;
494
495 if (! file)
496 return;
497 for (i = 0; i < num_nodes; i++)
498 {
499 node_sched_params_ptr nsp = SCHED_PARAMS (i);
500
501 fprintf (file, "Node = %d; INSN = %d\n", i,
502 INSN_UID (ps_rtl_insn (ps, i)));
503 fprintf (file, " asap = %d:\n", NODE_ASAP (&ps->g->nodes[i]));
504 fprintf (file, " time = %d:\n", nsp->time);
505 fprintf (file, " stage = %d:\n", nsp->stage);
506 }
507 }
508
509 /* Set SCHED_COLUMN for each instruction in row ROW of PS. */
510 static void
511 set_columns_for_row (partial_schedule_ptr ps, int row)
512 {
513 ps_insn_ptr cur_insn;
514 int column;
515
516 column = 0;
517 for (cur_insn = ps->rows[row]; cur_insn; cur_insn = cur_insn->next_in_row)
518 SCHED_COLUMN (cur_insn->id) = column++;
519 }
520
521 /* Set SCHED_COLUMN for each instruction in PS. */
522 static void
523 set_columns_for_ps (partial_schedule_ptr ps)
524 {
525 int row;
526
527 for (row = 0; row < ps->ii; row++)
528 set_columns_for_row (ps, row);
529 }
530
531 /* Try to schedule the move with ps_insn identifier I_REG_MOVE in PS.
532 Its single predecessor has already been scheduled, as has its
533 ddg node successors. (The move may have also another move as its
534 successor, in which case that successor will be scheduled later.)
535
536 The move is part of a chain that satisfies register dependencies
537 between a producing ddg node and various consuming ddg nodes.
538 If some of these dependencies have a distance of 1 (meaning that
539 the use is upward-exposed) then DISTANCE1_USES is nonnull and
540 contains the set of uses with distance-1 dependencies.
541 DISTANCE1_USES is null otherwise.
542
543 MUST_FOLLOW is a scratch bitmap that is big enough to hold
544 all current ps_insn ids.
545
546 Return true on success. */
547 static bool
548 schedule_reg_move (partial_schedule_ptr ps, int i_reg_move,
549 sbitmap distance1_uses, sbitmap must_follow)
550 {
551 unsigned int u;
552 int this_time, this_distance, this_start, this_end, this_latency;
553 int start, end, c, ii;
554 sbitmap_iterator sbi;
555 ps_reg_move_info *move;
556 rtx this_insn;
557 ps_insn_ptr psi;
558
559 move = ps_reg_move (ps, i_reg_move);
560 ii = ps->ii;
561 if (dump_file)
562 {
563 fprintf (dump_file, "Scheduling register move INSN %d; ii = %d"
564 ", min cycle = %d\n\n", INSN_UID (move->insn), ii,
565 PS_MIN_CYCLE (ps));
566 print_rtl_single (dump_file, move->insn);
567 fprintf (dump_file, "\n%11s %11s %5s\n", "start", "end", "time");
568 fprintf (dump_file, "=========== =========== =====\n");
569 }
570
571 start = INT_MIN;
572 end = INT_MAX;
573
574 /* For dependencies of distance 1 between a producer ddg node A
575 and consumer ddg node B, we have a chain of dependencies:
576
577 A --(T,L1,1)--> M1 --(T,L2,0)--> M2 ... --(T,Ln,0)--> B
578
579 where Mi is the ith move. For dependencies of distance 0 between
580 a producer ddg node A and consumer ddg node C, we have a chain of
581 dependencies:
582
583 A --(T,L1',0)--> M1' --(T,L2',0)--> M2' ... --(T,Ln',0)--> C
584
585 where Mi' occupies the same position as Mi but occurs a stage later.
586 We can only schedule each move once, so if we have both types of
587 chain, we model the second as:
588
589 A --(T,L1',1)--> M1 --(T,L2',0)--> M2 ... --(T,Ln',-1)--> C
590
591 First handle the dependencies between the previously-scheduled
592 predecessor and the move. */
593 this_insn = ps_rtl_insn (ps, move->def);
594 this_latency = insn_latency (this_insn, move->insn);
595 this_distance = distance1_uses && move->def < ps->g->num_nodes ? 1 : 0;
596 this_time = SCHED_TIME (move->def) - this_distance * ii;
597 this_start = this_time + this_latency;
598 this_end = this_time + ii;
599 if (dump_file)
600 fprintf (dump_file, "%11d %11d %5d %d --(T,%d,%d)--> %d\n",
601 this_start, this_end, SCHED_TIME (move->def),
602 INSN_UID (this_insn), this_latency, this_distance,
603 INSN_UID (move->insn));
604
605 if (start < this_start)
606 start = this_start;
607 if (end > this_end)
608 end = this_end;
609
610 /* Handle the dependencies between the move and previously-scheduled
611 successors. */
612 EXECUTE_IF_SET_IN_BITMAP (move->uses, 0, u, sbi)
613 {
614 this_insn = ps_rtl_insn (ps, u);
615 this_latency = insn_latency (move->insn, this_insn);
616 if (distance1_uses && !bitmap_bit_p (distance1_uses, u))
617 this_distance = -1;
618 else
619 this_distance = 0;
620 this_time = SCHED_TIME (u) + this_distance * ii;
621 this_start = this_time - ii;
622 this_end = this_time - this_latency;
623 if (dump_file)
624 fprintf (dump_file, "%11d %11d %5d %d --(T,%d,%d)--> %d\n",
625 this_start, this_end, SCHED_TIME (u), INSN_UID (move->insn),
626 this_latency, this_distance, INSN_UID (this_insn));
627
628 if (start < this_start)
629 start = this_start;
630 if (end > this_end)
631 end = this_end;
632 }
633
634 if (dump_file)
635 {
636 fprintf (dump_file, "----------- ----------- -----\n");
637 fprintf (dump_file, "%11d %11d %5s %s\n", start, end, "", "(max, min)");
638 }
639
640 bitmap_clear (must_follow);
641 bitmap_set_bit (must_follow, move->def);
642
643 start = MAX (start, end - (ii - 1));
644 for (c = end; c >= start; c--)
645 {
646 psi = ps_add_node_check_conflicts (ps, i_reg_move, c,
647 move->uses, must_follow);
648 if (psi)
649 {
650 update_node_sched_params (i_reg_move, ii, c, PS_MIN_CYCLE (ps));
651 if (dump_file)
652 fprintf (dump_file, "\nScheduled register move INSN %d at"
653 " time %d, row %d\n\n", INSN_UID (move->insn), c,
654 SCHED_ROW (i_reg_move));
655 return true;
656 }
657 }
658
659 if (dump_file)
660 fprintf (dump_file, "\nNo available slot\n\n");
661
662 return false;
663 }
664
665 /*
666 Breaking intra-loop register anti-dependences:
667 Each intra-loop register anti-dependence implies a cross-iteration true
668 dependence of distance 1. Therefore, we can remove such false dependencies
669 and figure out if the partial schedule broke them by checking if (for a
670 true-dependence of distance 1): SCHED_TIME (def) < SCHED_TIME (use) and
671 if so generate a register move. The number of such moves is equal to:
672 SCHED_TIME (use) - SCHED_TIME (def) { 0 broken
673 nreg_moves = ----------------------------------- + 1 - { dependence.
674 ii { 1 if not.
675 */
676 static bool
677 schedule_reg_moves (partial_schedule_ptr ps)
678 {
679 ddg_ptr g = ps->g;
680 int ii = ps->ii;
681 int i;
682
683 for (i = 0; i < g->num_nodes; i++)
684 {
685 ddg_node_ptr u = &g->nodes[i];
686 ddg_edge_ptr e;
687 int nreg_moves = 0, i_reg_move;
688 rtx prev_reg, old_reg;
689 int first_move;
690 int distances[2];
691 sbitmap must_follow;
692 sbitmap distance1_uses;
693 rtx set = single_set (u->insn);
694
695 /* Skip instructions that do not set a register. */
696 if ((set && !REG_P (SET_DEST (set))))
697 continue;
698
699 /* Compute the number of reg_moves needed for u, by looking at life
700 ranges started at u (excluding self-loops). */
701 distances[0] = distances[1] = false;
702 for (e = u->out; e; e = e->next_out)
703 if (e->type == TRUE_DEP && e->dest != e->src)
704 {
705 int nreg_moves4e = (SCHED_TIME (e->dest->cuid)
706 - SCHED_TIME (e->src->cuid)) / ii;
707
708 if (e->distance == 1)
709 nreg_moves4e = (SCHED_TIME (e->dest->cuid)
710 - SCHED_TIME (e->src->cuid) + ii) / ii;
711
712 /* If dest precedes src in the schedule of the kernel, then dest
713 will read before src writes and we can save one reg_copy. */
714 if (SCHED_ROW (e->dest->cuid) == SCHED_ROW (e->src->cuid)
715 && SCHED_COLUMN (e->dest->cuid) < SCHED_COLUMN (e->src->cuid))
716 nreg_moves4e--;
717
718 if (nreg_moves4e >= 1)
719 {
720 /* !single_set instructions are not supported yet and
721 thus we do not except to encounter them in the loop
722 except from the doloop part. For the latter case
723 we assume no regmoves are generated as the doloop
724 instructions are tied to the branch with an edge. */
725 gcc_assert (set);
726 /* If the instruction contains auto-inc register then
727 validate that the regmov is being generated for the
728 target regsiter rather then the inc'ed register. */
729 gcc_assert (!autoinc_var_is_used_p (u->insn, e->dest->insn));
730 }
731
732 if (nreg_moves4e)
733 {
734 gcc_assert (e->distance < 2);
735 distances[e->distance] = true;
736 }
737 nreg_moves = MAX (nreg_moves, nreg_moves4e);
738 }
739
740 if (nreg_moves == 0)
741 continue;
742
743 /* Create NREG_MOVES register moves. */
744 first_move = ps->reg_moves.length ();
745 ps->reg_moves.safe_grow_cleared (first_move + nreg_moves);
746 extend_node_sched_params (ps);
747
748 /* Record the moves associated with this node. */
749 first_move += ps->g->num_nodes;
750
751 /* Generate each move. */
752 old_reg = prev_reg = SET_DEST (single_set (u->insn));
753 for (i_reg_move = 0; i_reg_move < nreg_moves; i_reg_move++)
754 {
755 ps_reg_move_info *move = ps_reg_move (ps, first_move + i_reg_move);
756
757 move->def = i_reg_move > 0 ? first_move + i_reg_move - 1 : i;
758 move->uses = sbitmap_alloc (first_move + nreg_moves);
759 move->old_reg = old_reg;
760 move->new_reg = gen_reg_rtx (GET_MODE (prev_reg));
761 move->num_consecutive_stages = distances[0] && distances[1] ? 2 : 1;
762 move->insn = gen_move_insn (move->new_reg, copy_rtx (prev_reg));
763 bitmap_clear (move->uses);
764
765 prev_reg = move->new_reg;
766 }
767
768 distance1_uses = distances[1] ? sbitmap_alloc (g->num_nodes) : NULL;
769
770 /* Every use of the register defined by node may require a different
771 copy of this register, depending on the time the use is scheduled.
772 Record which uses require which move results. */
773 for (e = u->out; e; e = e->next_out)
774 if (e->type == TRUE_DEP && e->dest != e->src)
775 {
776 int dest_copy = (SCHED_TIME (e->dest->cuid)
777 - SCHED_TIME (e->src->cuid)) / ii;
778
779 if (e->distance == 1)
780 dest_copy = (SCHED_TIME (e->dest->cuid)
781 - SCHED_TIME (e->src->cuid) + ii) / ii;
782
783 if (SCHED_ROW (e->dest->cuid) == SCHED_ROW (e->src->cuid)
784 && SCHED_COLUMN (e->dest->cuid) < SCHED_COLUMN (e->src->cuid))
785 dest_copy--;
786
787 if (dest_copy)
788 {
789 ps_reg_move_info *move;
790
791 move = ps_reg_move (ps, first_move + dest_copy - 1);
792 bitmap_set_bit (move->uses, e->dest->cuid);
793 if (e->distance == 1)
794 bitmap_set_bit (distance1_uses, e->dest->cuid);
795 }
796 }
797
798 must_follow = sbitmap_alloc (first_move + nreg_moves);
799 for (i_reg_move = 0; i_reg_move < nreg_moves; i_reg_move++)
800 if (!schedule_reg_move (ps, first_move + i_reg_move,
801 distance1_uses, must_follow))
802 break;
803 sbitmap_free (must_follow);
804 if (distance1_uses)
805 sbitmap_free (distance1_uses);
806 if (i_reg_move < nreg_moves)
807 return false;
808 }
809 return true;
810 }
811
812 /* Emit the moves associatied with PS. Apply the substitutions
813 associated with them. */
814 static void
815 apply_reg_moves (partial_schedule_ptr ps)
816 {
817 ps_reg_move_info *move;
818 int i;
819
820 FOR_EACH_VEC_ELT (ps->reg_moves, i, move)
821 {
822 unsigned int i_use;
823 sbitmap_iterator sbi;
824
825 EXECUTE_IF_SET_IN_BITMAP (move->uses, 0, i_use, sbi)
826 {
827 replace_rtx (ps->g->nodes[i_use].insn, move->old_reg, move->new_reg);
828 df_insn_rescan (ps->g->nodes[i_use].insn);
829 }
830 }
831 }
832
833 /* Bump the SCHED_TIMEs of all nodes by AMOUNT. Set the values of
834 SCHED_ROW and SCHED_STAGE. Instruction scheduled on cycle AMOUNT
835 will move to cycle zero. */
836 static void
837 reset_sched_times (partial_schedule_ptr ps, int amount)
838 {
839 int row;
840 int ii = ps->ii;
841 ps_insn_ptr crr_insn;
842
843 for (row = 0; row < ii; row++)
844 for (crr_insn = ps->rows[row]; crr_insn; crr_insn = crr_insn->next_in_row)
845 {
846 int u = crr_insn->id;
847 int normalized_time = SCHED_TIME (u) - amount;
848 int new_min_cycle = PS_MIN_CYCLE (ps) - amount;
849
850 if (dump_file)
851 {
852 /* Print the scheduling times after the rotation. */
853 rtx insn = ps_rtl_insn (ps, u);
854
855 fprintf (dump_file, "crr_insn->node=%d (insn id %d), "
856 "crr_insn->cycle=%d, min_cycle=%d", u,
857 INSN_UID (insn), normalized_time, new_min_cycle);
858 if (JUMP_P (insn))
859 fprintf (dump_file, " (branch)");
860 fprintf (dump_file, "\n");
861 }
862
863 gcc_assert (SCHED_TIME (u) >= ps->min_cycle);
864 gcc_assert (SCHED_TIME (u) <= ps->max_cycle);
865
866 crr_insn->cycle = normalized_time;
867 update_node_sched_params (u, ii, normalized_time, new_min_cycle);
868 }
869 }
870
871 /* Permute the insns according to their order in PS, from row 0 to
872 row ii-1, and position them right before LAST. This schedules
873 the insns of the loop kernel. */
874 static void
875 permute_partial_schedule (partial_schedule_ptr ps, rtx last)
876 {
877 int ii = ps->ii;
878 int row;
879 ps_insn_ptr ps_ij;
880
881 for (row = 0; row < ii ; row++)
882 for (ps_ij = ps->rows[row]; ps_ij; ps_ij = ps_ij->next_in_row)
883 {
884 rtx insn = ps_rtl_insn (ps, ps_ij->id);
885
886 if (PREV_INSN (last) != insn)
887 {
888 if (ps_ij->id < ps->g->num_nodes)
889 reorder_insns_nobb (ps_first_note (ps, ps_ij->id), insn,
890 PREV_INSN (last));
891 else
892 add_insn_before (insn, last, NULL);
893 }
894 }
895 }
896
897 /* Set bitmaps TMP_FOLLOW and TMP_PRECEDE to MUST_FOLLOW and MUST_PRECEDE
898 respectively only if cycle C falls on the border of the scheduling
899 window boundaries marked by START and END cycles. STEP is the
900 direction of the window. */
901 static inline void
902 set_must_precede_follow (sbitmap *tmp_follow, sbitmap must_follow,
903 sbitmap *tmp_precede, sbitmap must_precede, int c,
904 int start, int end, int step)
905 {
906 *tmp_precede = NULL;
907 *tmp_follow = NULL;
908
909 if (c == start)
910 {
911 if (step == 1)
912 *tmp_precede = must_precede;
913 else /* step == -1. */
914 *tmp_follow = must_follow;
915 }
916 if (c == end - step)
917 {
918 if (step == 1)
919 *tmp_follow = must_follow;
920 else /* step == -1. */
921 *tmp_precede = must_precede;
922 }
923
924 }
925
926 /* Return True if the branch can be moved to row ii-1 while
927 normalizing the partial schedule PS to start from cycle zero and thus
928 optimize the SC. Otherwise return False. */
929 static bool
930 optimize_sc (partial_schedule_ptr ps, ddg_ptr g)
931 {
932 int amount = PS_MIN_CYCLE (ps);
933 sbitmap sched_nodes = sbitmap_alloc (g->num_nodes);
934 int start, end, step;
935 int ii = ps->ii;
936 bool ok = false;
937 int stage_count, stage_count_curr;
938
939 /* Compare the SC after normalization and SC after bringing the branch
940 to row ii-1. If they are equal just bail out. */
941 stage_count = calculate_stage_count (ps, amount);
942 stage_count_curr =
943 calculate_stage_count (ps, SCHED_TIME (g->closing_branch->cuid) - (ii - 1));
944
945 if (stage_count == stage_count_curr)
946 {
947 if (dump_file)
948 fprintf (dump_file, "SMS SC already optimized.\n");
949
950 ok = false;
951 goto clear;
952 }
953
954 if (dump_file)
955 {
956 fprintf (dump_file, "SMS Trying to optimize branch location\n");
957 fprintf (dump_file, "SMS partial schedule before trial:\n");
958 print_partial_schedule (ps, dump_file);
959 }
960
961 /* First, normalize the partial scheduling. */
962 reset_sched_times (ps, amount);
963 rotate_partial_schedule (ps, amount);
964 if (dump_file)
965 {
966 fprintf (dump_file,
967 "SMS partial schedule after normalization (ii, %d, SC %d):\n",
968 ii, stage_count);
969 print_partial_schedule (ps, dump_file);
970 }
971
972 if (SMODULO (SCHED_TIME (g->closing_branch->cuid), ii) == ii - 1)
973 {
974 ok = true;
975 goto clear;
976 }
977
978 bitmap_ones (sched_nodes);
979
980 /* Calculate the new placement of the branch. It should be in row
981 ii-1 and fall into it's scheduling window. */
982 if (get_sched_window (ps, g->closing_branch, sched_nodes, ii, &start,
983 &step, &end) == 0)
984 {
985 bool success;
986 ps_insn_ptr next_ps_i;
987 int branch_cycle = SCHED_TIME (g->closing_branch->cuid);
988 int row = SMODULO (branch_cycle, ps->ii);
989 int num_splits = 0;
990 sbitmap must_precede, must_follow, tmp_precede, tmp_follow;
991 int c;
992
993 if (dump_file)
994 fprintf (dump_file, "\nTrying to schedule node %d "
995 "INSN = %d in (%d .. %d) step %d\n",
996 g->closing_branch->cuid,
997 (INSN_UID (g->closing_branch->insn)), start, end, step);
998
999 gcc_assert ((step > 0 && start < end) || (step < 0 && start > end));
1000 if (step == 1)
1001 {
1002 c = start + ii - SMODULO (start, ii) - 1;
1003 gcc_assert (c >= start);
1004 if (c >= end)
1005 {
1006 ok = false;
1007 if (dump_file)
1008 fprintf (dump_file,
1009 "SMS failed to schedule branch at cycle: %d\n", c);
1010 goto clear;
1011 }
1012 }
1013 else
1014 {
1015 c = start - SMODULO (start, ii) - 1;
1016 gcc_assert (c <= start);
1017
1018 if (c <= end)
1019 {
1020 if (dump_file)
1021 fprintf (dump_file,
1022 "SMS failed to schedule branch at cycle: %d\n", c);
1023 ok = false;
1024 goto clear;
1025 }
1026 }
1027
1028 must_precede = sbitmap_alloc (g->num_nodes);
1029 must_follow = sbitmap_alloc (g->num_nodes);
1030
1031 /* Try to schedule the branch is it's new cycle. */
1032 calculate_must_precede_follow (g->closing_branch, start, end,
1033 step, ii, sched_nodes,
1034 must_precede, must_follow);
1035
1036 set_must_precede_follow (&tmp_follow, must_follow, &tmp_precede,
1037 must_precede, c, start, end, step);
1038
1039 /* Find the element in the partial schedule related to the closing
1040 branch so we can remove it from it's current cycle. */
1041 for (next_ps_i = ps->rows[row];
1042 next_ps_i; next_ps_i = next_ps_i->next_in_row)
1043 if (next_ps_i->id == g->closing_branch->cuid)
1044 break;
1045
1046 remove_node_from_ps (ps, next_ps_i);
1047 success =
1048 try_scheduling_node_in_cycle (ps, g->closing_branch->cuid, c,
1049 sched_nodes, &num_splits,
1050 tmp_precede, tmp_follow);
1051 gcc_assert (num_splits == 0);
1052 if (!success)
1053 {
1054 if (dump_file)
1055 fprintf (dump_file,
1056 "SMS failed to schedule branch at cycle: %d, "
1057 "bringing it back to cycle %d\n", c, branch_cycle);
1058
1059 /* The branch was failed to be placed in row ii - 1.
1060 Put it back in it's original place in the partial
1061 schedualing. */
1062 set_must_precede_follow (&tmp_follow, must_follow, &tmp_precede,
1063 must_precede, branch_cycle, start, end,
1064 step);
1065 success =
1066 try_scheduling_node_in_cycle (ps, g->closing_branch->cuid,
1067 branch_cycle, sched_nodes,
1068 &num_splits, tmp_precede,
1069 tmp_follow);
1070 gcc_assert (success && (num_splits == 0));
1071 ok = false;
1072 }
1073 else
1074 {
1075 /* The branch is placed in row ii - 1. */
1076 if (dump_file)
1077 fprintf (dump_file,
1078 "SMS success in moving branch to cycle %d\n", c);
1079
1080 update_node_sched_params (g->closing_branch->cuid, ii, c,
1081 PS_MIN_CYCLE (ps));
1082 ok = true;
1083 }
1084
1085 free (must_precede);
1086 free (must_follow);
1087 }
1088
1089 clear:
1090 free (sched_nodes);
1091 return ok;
1092 }
1093
1094 static void
1095 duplicate_insns_of_cycles (partial_schedule_ptr ps, int from_stage,
1096 int to_stage, rtx count_reg)
1097 {
1098 int row;
1099 ps_insn_ptr ps_ij;
1100
1101 for (row = 0; row < ps->ii; row++)
1102 for (ps_ij = ps->rows[row]; ps_ij; ps_ij = ps_ij->next_in_row)
1103 {
1104 int u = ps_ij->id;
1105 int first_u, last_u;
1106 rtx u_insn;
1107
1108 /* Do not duplicate any insn which refers to count_reg as it
1109 belongs to the control part.
1110 The closing branch is scheduled as well and thus should
1111 be ignored.
1112 TODO: This should be done by analyzing the control part of
1113 the loop. */
1114 u_insn = ps_rtl_insn (ps, u);
1115 if (reg_mentioned_p (count_reg, u_insn)
1116 || JUMP_P (u_insn))
1117 continue;
1118
1119 first_u = SCHED_STAGE (u);
1120 last_u = first_u + ps_num_consecutive_stages (ps, u) - 1;
1121 if (from_stage <= last_u && to_stage >= first_u)
1122 {
1123 if (u < ps->g->num_nodes)
1124 duplicate_insn_chain (ps_first_note (ps, u), u_insn);
1125 else
1126 emit_insn (copy_rtx (PATTERN (u_insn)));
1127 }
1128 }
1129 }
1130
1131
1132 /* Generate the instructions (including reg_moves) for prolog & epilog. */
1133 static void
1134 generate_prolog_epilog (partial_schedule_ptr ps, struct loop *loop,
1135 rtx count_reg, rtx count_init)
1136 {
1137 int i;
1138 int last_stage = PS_STAGE_COUNT (ps) - 1;
1139 edge e;
1140
1141 /* Generate the prolog, inserting its insns on the loop-entry edge. */
1142 start_sequence ();
1143
1144 if (!count_init)
1145 {
1146 /* Generate instructions at the beginning of the prolog to
1147 adjust the loop count by STAGE_COUNT. If loop count is constant
1148 (count_init), this constant is adjusted by STAGE_COUNT in
1149 generate_prolog_epilog function. */
1150 rtx sub_reg = NULL_RTX;
1151
1152 sub_reg = expand_simple_binop (GET_MODE (count_reg), MINUS,
1153 count_reg, GEN_INT (last_stage),
1154 count_reg, 1, OPTAB_DIRECT);
1155 gcc_assert (REG_P (sub_reg));
1156 if (REGNO (sub_reg) != REGNO (count_reg))
1157 emit_move_insn (count_reg, sub_reg);
1158 }
1159
1160 for (i = 0; i < last_stage; i++)
1161 duplicate_insns_of_cycles (ps, 0, i, count_reg);
1162
1163 /* Put the prolog on the entry edge. */
1164 e = loop_preheader_edge (loop);
1165 split_edge_and_insert (e, get_insns ());
1166 if (!flag_resched_modulo_sched)
1167 e->dest->flags |= BB_DISABLE_SCHEDULE;
1168
1169 end_sequence ();
1170
1171 /* Generate the epilog, inserting its insns on the loop-exit edge. */
1172 start_sequence ();
1173
1174 for (i = 0; i < last_stage; i++)
1175 duplicate_insns_of_cycles (ps, i + 1, last_stage, count_reg);
1176
1177 /* Put the epilogue on the exit edge. */
1178 gcc_assert (single_exit (loop));
1179 e = single_exit (loop);
1180 split_edge_and_insert (e, get_insns ());
1181 if (!flag_resched_modulo_sched)
1182 e->dest->flags |= BB_DISABLE_SCHEDULE;
1183
1184 end_sequence ();
1185 }
1186
1187 /* Mark LOOP as software pipelined so the later
1188 scheduling passes don't touch it. */
1189 static void
1190 mark_loop_unsched (struct loop *loop)
1191 {
1192 unsigned i;
1193 basic_block *bbs = get_loop_body (loop);
1194
1195 for (i = 0; i < loop->num_nodes; i++)
1196 bbs[i]->flags |= BB_DISABLE_SCHEDULE;
1197
1198 free (bbs);
1199 }
1200
1201 /* Return true if all the BBs of the loop are empty except the
1202 loop header. */
1203 static bool
1204 loop_single_full_bb_p (struct loop *loop)
1205 {
1206 unsigned i;
1207 basic_block *bbs = get_loop_body (loop);
1208
1209 for (i = 0; i < loop->num_nodes ; i++)
1210 {
1211 rtx head, tail;
1212 bool empty_bb = true;
1213
1214 if (bbs[i] == loop->header)
1215 continue;
1216
1217 /* Make sure that basic blocks other than the header
1218 have only notes labels or jumps. */
1219 get_ebb_head_tail (bbs[i], bbs[i], &head, &tail);
1220 for (; head != NEXT_INSN (tail); head = NEXT_INSN (head))
1221 {
1222 if (NOTE_P (head) || LABEL_P (head)
1223 || (INSN_P (head) && (DEBUG_INSN_P (head) || JUMP_P (head))))
1224 continue;
1225 empty_bb = false;
1226 break;
1227 }
1228
1229 if (! empty_bb)
1230 {
1231 free (bbs);
1232 return false;
1233 }
1234 }
1235 free (bbs);
1236 return true;
1237 }
1238
1239 /* Dump file:line from INSN's location info to dump_file. */
1240
1241 static void
1242 dump_insn_location (rtx insn)
1243 {
1244 if (dump_file && INSN_LOCATION (insn))
1245 {
1246 const char *file = insn_file (insn);
1247 if (file)
1248 fprintf (dump_file, " %s:%i", file, insn_line (insn));
1249 }
1250 }
1251
1252 /* A simple loop from SMS point of view; it is a loop that is composed of
1253 either a single basic block or two BBs - a header and a latch. */
1254 #define SIMPLE_SMS_LOOP_P(loop) ((loop->num_nodes < 3 ) \
1255 && (EDGE_COUNT (loop->latch->preds) == 1) \
1256 && (EDGE_COUNT (loop->latch->succs) == 1))
1257
1258 /* Return true if the loop is in its canonical form and false if not.
1259 i.e. SIMPLE_SMS_LOOP_P and have one preheader block, and single exit. */
1260 static bool
1261 loop_canon_p (struct loop *loop)
1262 {
1263
1264 if (loop->inner || !loop_outer (loop))
1265 {
1266 if (dump_file)
1267 fprintf (dump_file, "SMS loop inner or !loop_outer\n");
1268 return false;
1269 }
1270
1271 if (!single_exit (loop))
1272 {
1273 if (dump_file)
1274 {
1275 rtx insn = BB_END (loop->header);
1276
1277 fprintf (dump_file, "SMS loop many exits");
1278 dump_insn_location (insn);
1279 fprintf (dump_file, "\n");
1280 }
1281 return false;
1282 }
1283
1284 if (! SIMPLE_SMS_LOOP_P (loop) && ! loop_single_full_bb_p (loop))
1285 {
1286 if (dump_file)
1287 {
1288 rtx insn = BB_END (loop->header);
1289
1290 fprintf (dump_file, "SMS loop many BBs.");
1291 dump_insn_location (insn);
1292 fprintf (dump_file, "\n");
1293 }
1294 return false;
1295 }
1296
1297 return true;
1298 }
1299
1300 /* If there are more than one entry for the loop,
1301 make it one by splitting the first entry edge and
1302 redirecting the others to the new BB. */
1303 static void
1304 canon_loop (struct loop *loop)
1305 {
1306 edge e;
1307 edge_iterator i;
1308
1309 /* Avoid annoying special cases of edges going to exit
1310 block. */
1311 FOR_EACH_EDGE (e, i, EXIT_BLOCK_PTR->preds)
1312 if ((e->flags & EDGE_FALLTHRU) && (EDGE_COUNT (e->src->succs) > 1))
1313 split_edge (e);
1314
1315 if (loop->latch == loop->header
1316 || EDGE_COUNT (loop->latch->succs) > 1)
1317 {
1318 FOR_EACH_EDGE (e, i, loop->header->preds)
1319 if (e->src == loop->latch)
1320 break;
1321 split_edge (e);
1322 }
1323 }
1324
1325 /* Setup infos. */
1326 static void
1327 setup_sched_infos (void)
1328 {
1329 memcpy (&sms_common_sched_info, &haifa_common_sched_info,
1330 sizeof (sms_common_sched_info));
1331 sms_common_sched_info.sched_pass_id = SCHED_SMS_PASS;
1332 common_sched_info = &sms_common_sched_info;
1333
1334 sched_deps_info = &sms_sched_deps_info;
1335 current_sched_info = &sms_sched_info;
1336 }
1337
1338 /* Probability in % that the sms-ed loop rolls enough so that optimized
1339 version may be entered. Just a guess. */
1340 #define PROB_SMS_ENOUGH_ITERATIONS 80
1341
1342 /* Used to calculate the upper bound of ii. */
1343 #define MAXII_FACTOR 2
1344
1345 /* Main entry point, perform SMS scheduling on the loops of the function
1346 that consist of single basic blocks. */
1347 static void
1348 sms_schedule (void)
1349 {
1350 rtx insn;
1351 ddg_ptr *g_arr, g;
1352 int * node_order;
1353 int maxii, max_asap;
1354 loop_iterator li;
1355 partial_schedule_ptr ps;
1356 basic_block bb = NULL;
1357 struct loop *loop;
1358 basic_block condition_bb = NULL;
1359 edge latch_edge;
1360 gcov_type trip_count = 0;
1361
1362 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
1363 | LOOPS_HAVE_RECORDED_EXITS);
1364 if (number_of_loops () <= 1)
1365 {
1366 loop_optimizer_finalize ();
1367 return; /* There are no loops to schedule. */
1368 }
1369
1370 /* Initialize issue_rate. */
1371 if (targetm.sched.issue_rate)
1372 {
1373 int temp = reload_completed;
1374
1375 reload_completed = 1;
1376 issue_rate = targetm.sched.issue_rate ();
1377 reload_completed = temp;
1378 }
1379 else
1380 issue_rate = 1;
1381
1382 /* Initialize the scheduler. */
1383 setup_sched_infos ();
1384 haifa_sched_init ();
1385
1386 /* Allocate memory to hold the DDG array one entry for each loop.
1387 We use loop->num as index into this array. */
1388 g_arr = XCNEWVEC (ddg_ptr, number_of_loops ());
1389
1390 if (dump_file)
1391 {
1392 fprintf (dump_file, "\n\nSMS analysis phase\n");
1393 fprintf (dump_file, "===================\n\n");
1394 }
1395
1396 /* Build DDGs for all the relevant loops and hold them in G_ARR
1397 indexed by the loop index. */
1398 FOR_EACH_LOOP (li, loop, 0)
1399 {
1400 rtx head, tail;
1401 rtx count_reg;
1402
1403 /* For debugging. */
1404 if (dbg_cnt (sms_sched_loop) == false)
1405 {
1406 if (dump_file)
1407 fprintf (dump_file, "SMS reached max limit... \n");
1408
1409 FOR_EACH_LOOP_BREAK (li);
1410 }
1411
1412 if (dump_file)
1413 {
1414 rtx insn = BB_END (loop->header);
1415
1416 fprintf (dump_file, "SMS loop num: %d", loop->num);
1417 dump_insn_location (insn);
1418 fprintf (dump_file, "\n");
1419 }
1420
1421 if (! loop_canon_p (loop))
1422 continue;
1423
1424 if (! loop_single_full_bb_p (loop))
1425 {
1426 if (dump_file)
1427 fprintf (dump_file, "SMS not loop_single_full_bb_p\n");
1428 continue;
1429 }
1430
1431 bb = loop->header;
1432
1433 get_ebb_head_tail (bb, bb, &head, &tail);
1434 latch_edge = loop_latch_edge (loop);
1435 gcc_assert (single_exit (loop));
1436 if (single_exit (loop)->count)
1437 trip_count = latch_edge->count / single_exit (loop)->count;
1438
1439 /* Perform SMS only on loops that their average count is above threshold. */
1440
1441 if ( latch_edge->count
1442 && (latch_edge->count < single_exit (loop)->count * SMS_LOOP_AVERAGE_COUNT_THRESHOLD))
1443 {
1444 if (dump_file)
1445 {
1446 dump_insn_location (tail);
1447 fprintf (dump_file, "\nSMS single-bb-loop\n");
1448 if (profile_info && flag_branch_probabilities)
1449 {
1450 fprintf (dump_file, "SMS loop-count ");
1451 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1452 (HOST_WIDEST_INT) bb->count);
1453 fprintf (dump_file, "\n");
1454 fprintf (dump_file, "SMS trip-count ");
1455 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1456 (HOST_WIDEST_INT) trip_count);
1457 fprintf (dump_file, "\n");
1458 fprintf (dump_file, "SMS profile-sum-max ");
1459 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1460 (HOST_WIDEST_INT) profile_info->sum_max);
1461 fprintf (dump_file, "\n");
1462 }
1463 }
1464 continue;
1465 }
1466
1467 /* Make sure this is a doloop. */
1468 if ( !(count_reg = doloop_register_get (head, tail)))
1469 {
1470 if (dump_file)
1471 fprintf (dump_file, "SMS doloop_register_get failed\n");
1472 continue;
1473 }
1474
1475 /* Don't handle BBs with calls or barriers
1476 or !single_set with the exception of instructions that include
1477 count_reg---these instructions are part of the control part
1478 that do-loop recognizes.
1479 ??? Should handle insns defining subregs. */
1480 for (insn = head; insn != NEXT_INSN (tail); insn = NEXT_INSN (insn))
1481 {
1482 rtx set;
1483
1484 if (CALL_P (insn)
1485 || BARRIER_P (insn)
1486 || (NONDEBUG_INSN_P (insn) && !JUMP_P (insn)
1487 && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE
1488 && !reg_mentioned_p (count_reg, insn))
1489 || (INSN_P (insn) && (set = single_set (insn))
1490 && GET_CODE (SET_DEST (set)) == SUBREG))
1491 break;
1492 }
1493
1494 if (insn != NEXT_INSN (tail))
1495 {
1496 if (dump_file)
1497 {
1498 if (CALL_P (insn))
1499 fprintf (dump_file, "SMS loop-with-call\n");
1500 else if (BARRIER_P (insn))
1501 fprintf (dump_file, "SMS loop-with-barrier\n");
1502 else if ((NONDEBUG_INSN_P (insn) && !JUMP_P (insn)
1503 && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE))
1504 fprintf (dump_file, "SMS loop-with-not-single-set\n");
1505 else
1506 fprintf (dump_file, "SMS loop with subreg in lhs\n");
1507 print_rtl_single (dump_file, insn);
1508 }
1509
1510 continue;
1511 }
1512
1513 /* Always schedule the closing branch with the rest of the
1514 instructions. The branch is rotated to be in row ii-1 at the
1515 end of the scheduling procedure to make sure it's the last
1516 instruction in the iteration. */
1517 if (! (g = create_ddg (bb, 1)))
1518 {
1519 if (dump_file)
1520 fprintf (dump_file, "SMS create_ddg failed\n");
1521 continue;
1522 }
1523
1524 g_arr[loop->num] = g;
1525 if (dump_file)
1526 fprintf (dump_file, "...OK\n");
1527
1528 }
1529 if (dump_file)
1530 {
1531 fprintf (dump_file, "\nSMS transformation phase\n");
1532 fprintf (dump_file, "=========================\n\n");
1533 }
1534
1535 /* We don't want to perform SMS on new loops - created by versioning. */
1536 FOR_EACH_LOOP (li, loop, 0)
1537 {
1538 rtx head, tail;
1539 rtx count_reg, count_init;
1540 int mii, rec_mii, stage_count, min_cycle;
1541 HOST_WIDEST_INT loop_count = 0;
1542 bool opt_sc_p;
1543
1544 if (! (g = g_arr[loop->num]))
1545 continue;
1546
1547 if (dump_file)
1548 {
1549 rtx insn = BB_END (loop->header);
1550
1551 fprintf (dump_file, "SMS loop num: %d", loop->num);
1552 dump_insn_location (insn);
1553 fprintf (dump_file, "\n");
1554
1555 print_ddg (dump_file, g);
1556 }
1557
1558 get_ebb_head_tail (loop->header, loop->header, &head, &tail);
1559
1560 latch_edge = loop_latch_edge (loop);
1561 gcc_assert (single_exit (loop));
1562 if (single_exit (loop)->count)
1563 trip_count = latch_edge->count / single_exit (loop)->count;
1564
1565 if (dump_file)
1566 {
1567 dump_insn_location (tail);
1568 fprintf (dump_file, "\nSMS single-bb-loop\n");
1569 if (profile_info && flag_branch_probabilities)
1570 {
1571 fprintf (dump_file, "SMS loop-count ");
1572 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1573 (HOST_WIDEST_INT) bb->count);
1574 fprintf (dump_file, "\n");
1575 fprintf (dump_file, "SMS profile-sum-max ");
1576 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1577 (HOST_WIDEST_INT) profile_info->sum_max);
1578 fprintf (dump_file, "\n");
1579 }
1580 fprintf (dump_file, "SMS doloop\n");
1581 fprintf (dump_file, "SMS built-ddg %d\n", g->num_nodes);
1582 fprintf (dump_file, "SMS num-loads %d\n", g->num_loads);
1583 fprintf (dump_file, "SMS num-stores %d\n", g->num_stores);
1584 }
1585
1586
1587 /* In case of th loop have doloop register it gets special
1588 handling. */
1589 count_init = NULL_RTX;
1590 if ((count_reg = doloop_register_get (head, tail)))
1591 {
1592 basic_block pre_header;
1593
1594 pre_header = loop_preheader_edge (loop)->src;
1595 count_init = const_iteration_count (count_reg, pre_header,
1596 &loop_count);
1597 }
1598 gcc_assert (count_reg);
1599
1600 if (dump_file && count_init)
1601 {
1602 fprintf (dump_file, "SMS const-doloop ");
1603 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1604 loop_count);
1605 fprintf (dump_file, "\n");
1606 }
1607
1608 node_order = XNEWVEC (int, g->num_nodes);
1609
1610 mii = 1; /* Need to pass some estimate of mii. */
1611 rec_mii = sms_order_nodes (g, mii, node_order, &max_asap);
1612 mii = MAX (res_MII (g), rec_mii);
1613 maxii = MAX (max_asap, MAXII_FACTOR * mii);
1614
1615 if (dump_file)
1616 fprintf (dump_file, "SMS iis %d %d %d (rec_mii, mii, maxii)\n",
1617 rec_mii, mii, maxii);
1618
1619 for (;;)
1620 {
1621 set_node_sched_params (g);
1622
1623 stage_count = 0;
1624 opt_sc_p = false;
1625 ps = sms_schedule_by_order (g, mii, maxii, node_order);
1626
1627 if (ps)
1628 {
1629 /* Try to achieve optimized SC by normalizing the partial
1630 schedule (having the cycles start from cycle zero).
1631 The branch location must be placed in row ii-1 in the
1632 final scheduling. If failed, shift all instructions to
1633 position the branch in row ii-1. */
1634 opt_sc_p = optimize_sc (ps, g);
1635 if (opt_sc_p)
1636 stage_count = calculate_stage_count (ps, 0);
1637 else
1638 {
1639 /* Bring the branch to cycle ii-1. */
1640 int amount = (SCHED_TIME (g->closing_branch->cuid)
1641 - (ps->ii - 1));
1642
1643 if (dump_file)
1644 fprintf (dump_file, "SMS schedule branch at cycle ii-1\n");
1645
1646 stage_count = calculate_stage_count (ps, amount);
1647 }
1648
1649 gcc_assert (stage_count >= 1);
1650 }
1651
1652 /* The default value of PARAM_SMS_MIN_SC is 2 as stage count of
1653 1 means that there is no interleaving between iterations thus
1654 we let the scheduling passes do the job in this case. */
1655 if (stage_count < PARAM_VALUE (PARAM_SMS_MIN_SC)
1656 || (count_init && (loop_count <= stage_count))
1657 || (flag_branch_probabilities && (trip_count <= stage_count)))
1658 {
1659 if (dump_file)
1660 {
1661 fprintf (dump_file, "SMS failed... \n");
1662 fprintf (dump_file, "SMS sched-failed (stage-count=%d,"
1663 " loop-count=", stage_count);
1664 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, loop_count);
1665 fprintf (dump_file, ", trip-count=");
1666 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, trip_count);
1667 fprintf (dump_file, ")\n");
1668 }
1669 break;
1670 }
1671
1672 if (!opt_sc_p)
1673 {
1674 /* Rotate the partial schedule to have the branch in row ii-1. */
1675 int amount = SCHED_TIME (g->closing_branch->cuid) - (ps->ii - 1);
1676
1677 reset_sched_times (ps, amount);
1678 rotate_partial_schedule (ps, amount);
1679 }
1680
1681 set_columns_for_ps (ps);
1682
1683 min_cycle = PS_MIN_CYCLE (ps) - SMODULO (PS_MIN_CYCLE (ps), ps->ii);
1684 if (!schedule_reg_moves (ps))
1685 {
1686 mii = ps->ii + 1;
1687 free_partial_schedule (ps);
1688 continue;
1689 }
1690
1691 /* Moves that handle incoming values might have been added
1692 to a new first stage. Bump the stage count if so.
1693
1694 ??? Perhaps we could consider rotating the schedule here
1695 instead? */
1696 if (PS_MIN_CYCLE (ps) < min_cycle)
1697 {
1698 reset_sched_times (ps, 0);
1699 stage_count++;
1700 }
1701
1702 /* The stage count should now be correct without rotation. */
1703 gcc_checking_assert (stage_count == calculate_stage_count (ps, 0));
1704 PS_STAGE_COUNT (ps) = stage_count;
1705
1706 canon_loop (loop);
1707
1708 if (dump_file)
1709 {
1710 dump_insn_location (tail);
1711 fprintf (dump_file, " SMS succeeded %d %d (with ii, sc)\n",
1712 ps->ii, stage_count);
1713 print_partial_schedule (ps, dump_file);
1714 }
1715
1716 /* case the BCT count is not known , Do loop-versioning */
1717 if (count_reg && ! count_init)
1718 {
1719 rtx comp_rtx = gen_rtx_fmt_ee (GT, VOIDmode, count_reg,
1720 GEN_INT(stage_count));
1721 unsigned prob = (PROB_SMS_ENOUGH_ITERATIONS
1722 * REG_BR_PROB_BASE) / 100;
1723
1724 loop_version (loop, comp_rtx, &condition_bb,
1725 prob, prob, REG_BR_PROB_BASE - prob,
1726 true);
1727 }
1728
1729 /* Set new iteration count of loop kernel. */
1730 if (count_reg && count_init)
1731 SET_SRC (single_set (count_init)) = GEN_INT (loop_count
1732 - stage_count + 1);
1733
1734 /* Now apply the scheduled kernel to the RTL of the loop. */
1735 permute_partial_schedule (ps, g->closing_branch->first_note);
1736
1737 /* Mark this loop as software pipelined so the later
1738 scheduling passes don't touch it. */
1739 if (! flag_resched_modulo_sched)
1740 mark_loop_unsched (loop);
1741
1742 /* The life-info is not valid any more. */
1743 df_set_bb_dirty (g->bb);
1744
1745 apply_reg_moves (ps);
1746 if (dump_file)
1747 print_node_sched_params (dump_file, g->num_nodes, ps);
1748 /* Generate prolog and epilog. */
1749 generate_prolog_epilog (ps, loop, count_reg, count_init);
1750 break;
1751 }
1752
1753 free_partial_schedule (ps);
1754 node_sched_param_vec.release ();
1755 free (node_order);
1756 free_ddg (g);
1757 }
1758
1759 free (g_arr);
1760
1761 /* Release scheduler data, needed until now because of DFA. */
1762 haifa_sched_finish ();
1763 loop_optimizer_finalize ();
1764 }
1765
1766 /* The SMS scheduling algorithm itself
1767 -----------------------------------
1768 Input: 'O' an ordered list of insns of a loop.
1769 Output: A scheduling of the loop - kernel, prolog, and epilogue.
1770
1771 'Q' is the empty Set
1772 'PS' is the partial schedule; it holds the currently scheduled nodes with
1773 their cycle/slot.
1774 'PSP' previously scheduled predecessors.
1775 'PSS' previously scheduled successors.
1776 't(u)' the cycle where u is scheduled.
1777 'l(u)' is the latency of u.
1778 'd(v,u)' is the dependence distance from v to u.
1779 'ASAP(u)' the earliest time at which u could be scheduled as computed in
1780 the node ordering phase.
1781 'check_hardware_resources_conflicts(u, PS, c)'
1782 run a trace around cycle/slot through DFA model
1783 to check resource conflicts involving instruction u
1784 at cycle c given the partial schedule PS.
1785 'add_to_partial_schedule_at_time(u, PS, c)'
1786 Add the node/instruction u to the partial schedule
1787 PS at time c.
1788 'calculate_register_pressure(PS)'
1789 Given a schedule of instructions, calculate the register
1790 pressure it implies. One implementation could be the
1791 maximum number of overlapping live ranges.
1792 'maxRP' The maximum allowed register pressure, it is usually derived from the number
1793 registers available in the hardware.
1794
1795 1. II = MII.
1796 2. PS = empty list
1797 3. for each node u in O in pre-computed order
1798 4. if (PSP(u) != Q && PSS(u) == Q) then
1799 5. Early_start(u) = max ( t(v) + l(v) - d(v,u)*II ) over all every v in PSP(u).
1800 6. start = Early_start; end = Early_start + II - 1; step = 1
1801 11. else if (PSP(u) == Q && PSS(u) != Q) then
1802 12. Late_start(u) = min ( t(v) - l(v) + d(v,u)*II ) over all every v in PSS(u).
1803 13. start = Late_start; end = Late_start - II + 1; step = -1
1804 14. else if (PSP(u) != Q && PSS(u) != Q) then
1805 15. Early_start(u) = max ( t(v) + l(v) - d(v,u)*II ) over all every v in PSP(u).
1806 16. Late_start(u) = min ( t(v) - l(v) + d(v,u)*II ) over all every v in PSS(u).
1807 17. start = Early_start;
1808 18. end = min(Early_start + II - 1 , Late_start);
1809 19. step = 1
1810 20. else "if (PSP(u) == Q && PSS(u) == Q)"
1811 21. start = ASAP(u); end = start + II - 1; step = 1
1812 22. endif
1813
1814 23. success = false
1815 24. for (c = start ; c != end ; c += step)
1816 25. if check_hardware_resources_conflicts(u, PS, c) then
1817 26. add_to_partial_schedule_at_time(u, PS, c)
1818 27. success = true
1819 28. break
1820 29. endif
1821 30. endfor
1822 31. if (success == false) then
1823 32. II = II + 1
1824 33. if (II > maxII) then
1825 34. finish - failed to schedule
1826 35. endif
1827 36. goto 2.
1828 37. endif
1829 38. endfor
1830 39. if (calculate_register_pressure(PS) > maxRP) then
1831 40. goto 32.
1832 41. endif
1833 42. compute epilogue & prologue
1834 43. finish - succeeded to schedule
1835
1836 ??? The algorithm restricts the scheduling window to II cycles.
1837 In rare cases, it may be better to allow windows of II+1 cycles.
1838 The window would then start and end on the same row, but with
1839 different "must precede" and "must follow" requirements. */
1840
1841 /* A limit on the number of cycles that resource conflicts can span. ??? Should
1842 be provided by DFA, and be dependent on the type of insn scheduled. Currently
1843 set to 0 to save compile time. */
1844 #define DFA_HISTORY SMS_DFA_HISTORY
1845
1846 /* A threshold for the number of repeated unsuccessful attempts to insert
1847 an empty row, before we flush the partial schedule and start over. */
1848 #define MAX_SPLIT_NUM 10
1849 /* Given the partial schedule PS, this function calculates and returns the
1850 cycles in which we can schedule the node with the given index I.
1851 NOTE: Here we do the backtracking in SMS, in some special cases. We have
1852 noticed that there are several cases in which we fail to SMS the loop
1853 because the sched window of a node is empty due to tight data-deps. In
1854 such cases we want to unschedule some of the predecessors/successors
1855 until we get non-empty scheduling window. It returns -1 if the
1856 scheduling window is empty and zero otherwise. */
1857
1858 static int
1859 get_sched_window (partial_schedule_ptr ps, ddg_node_ptr u_node,
1860 sbitmap sched_nodes, int ii, int *start_p, int *step_p,
1861 int *end_p)
1862 {
1863 int start, step, end;
1864 int early_start, late_start;
1865 ddg_edge_ptr e;
1866 sbitmap psp = sbitmap_alloc (ps->g->num_nodes);
1867 sbitmap pss = sbitmap_alloc (ps->g->num_nodes);
1868 sbitmap u_node_preds = NODE_PREDECESSORS (u_node);
1869 sbitmap u_node_succs = NODE_SUCCESSORS (u_node);
1870 int psp_not_empty;
1871 int pss_not_empty;
1872 int count_preds;
1873 int count_succs;
1874
1875 /* 1. compute sched window for u (start, end, step). */
1876 bitmap_clear (psp);
1877 bitmap_clear (pss);
1878 psp_not_empty = bitmap_and (psp, u_node_preds, sched_nodes);
1879 pss_not_empty = bitmap_and (pss, u_node_succs, sched_nodes);
1880
1881 /* We first compute a forward range (start <= end), then decide whether
1882 to reverse it. */
1883 early_start = INT_MIN;
1884 late_start = INT_MAX;
1885 start = INT_MIN;
1886 end = INT_MAX;
1887 step = 1;
1888
1889 count_preds = 0;
1890 count_succs = 0;
1891
1892 if (dump_file && (psp_not_empty || pss_not_empty))
1893 {
1894 fprintf (dump_file, "\nAnalyzing dependencies for node %d (INSN %d)"
1895 "; ii = %d\n\n", u_node->cuid, INSN_UID (u_node->insn), ii);
1896 fprintf (dump_file, "%11s %11s %11s %11s %5s\n",
1897 "start", "early start", "late start", "end", "time");
1898 fprintf (dump_file, "=========== =========== =========== ==========="
1899 " =====\n");
1900 }
1901 /* Calculate early_start and limit end. Both bounds are inclusive. */
1902 if (psp_not_empty)
1903 for (e = u_node->in; e != 0; e = e->next_in)
1904 {
1905 int v = e->src->cuid;
1906
1907 if (bitmap_bit_p (sched_nodes, v))
1908 {
1909 int p_st = SCHED_TIME (v);
1910 int earliest = p_st + e->latency - (e->distance * ii);
1911 int latest = (e->data_type == MEM_DEP ? p_st + ii - 1 : INT_MAX);
1912
1913 if (dump_file)
1914 {
1915 fprintf (dump_file, "%11s %11d %11s %11d %5d",
1916 "", earliest, "", latest, p_st);
1917 print_ddg_edge (dump_file, e);
1918 fprintf (dump_file, "\n");
1919 }
1920
1921 early_start = MAX (early_start, earliest);
1922 end = MIN (end, latest);
1923
1924 if (e->type == TRUE_DEP && e->data_type == REG_DEP)
1925 count_preds++;
1926 }
1927 }
1928
1929 /* Calculate late_start and limit start. Both bounds are inclusive. */
1930 if (pss_not_empty)
1931 for (e = u_node->out; e != 0; e = e->next_out)
1932 {
1933 int v = e->dest->cuid;
1934
1935 if (bitmap_bit_p (sched_nodes, v))
1936 {
1937 int s_st = SCHED_TIME (v);
1938 int earliest = (e->data_type == MEM_DEP ? s_st - ii + 1 : INT_MIN);
1939 int latest = s_st - e->latency + (e->distance * ii);
1940
1941 if (dump_file)
1942 {
1943 fprintf (dump_file, "%11d %11s %11d %11s %5d",
1944 earliest, "", latest, "", s_st);
1945 print_ddg_edge (dump_file, e);
1946 fprintf (dump_file, "\n");
1947 }
1948
1949 start = MAX (start, earliest);
1950 late_start = MIN (late_start, latest);
1951
1952 if (e->type == TRUE_DEP && e->data_type == REG_DEP)
1953 count_succs++;
1954 }
1955 }
1956
1957 if (dump_file && (psp_not_empty || pss_not_empty))
1958 {
1959 fprintf (dump_file, "----------- ----------- ----------- -----------"
1960 " -----\n");
1961 fprintf (dump_file, "%11d %11d %11d %11d %5s %s\n",
1962 start, early_start, late_start, end, "",
1963 "(max, max, min, min)");
1964 }
1965
1966 /* Get a target scheduling window no bigger than ii. */
1967 if (early_start == INT_MIN && late_start == INT_MAX)
1968 early_start = NODE_ASAP (u_node);
1969 else if (early_start == INT_MIN)
1970 early_start = late_start - (ii - 1);
1971 late_start = MIN (late_start, early_start + (ii - 1));
1972
1973 /* Apply memory dependence limits. */
1974 start = MAX (start, early_start);
1975 end = MIN (end, late_start);
1976
1977 if (dump_file && (psp_not_empty || pss_not_empty))
1978 fprintf (dump_file, "%11s %11d %11d %11s %5s final window\n",
1979 "", start, end, "", "");
1980
1981 /* If there are at least as many successors as predecessors, schedule the
1982 node close to its successors. */
1983 if (pss_not_empty && count_succs >= count_preds)
1984 {
1985 int tmp = end;
1986 end = start;
1987 start = tmp;
1988 step = -1;
1989 }
1990
1991 /* Now that we've finalized the window, make END an exclusive rather
1992 than an inclusive bound. */
1993 end += step;
1994
1995 *start_p = start;
1996 *step_p = step;
1997 *end_p = end;
1998 sbitmap_free (psp);
1999 sbitmap_free (pss);
2000
2001 if ((start >= end && step == 1) || (start <= end && step == -1))
2002 {
2003 if (dump_file)
2004 fprintf (dump_file, "\nEmpty window: start=%d, end=%d, step=%d\n",
2005 start, end, step);
2006 return -1;
2007 }
2008
2009 return 0;
2010 }
2011
2012 /* Calculate MUST_PRECEDE/MUST_FOLLOW bitmaps of U_NODE; which is the
2013 node currently been scheduled. At the end of the calculation
2014 MUST_PRECEDE/MUST_FOLLOW contains all predecessors/successors of
2015 U_NODE which are (1) already scheduled in the first/last row of
2016 U_NODE's scheduling window, (2) whose dependence inequality with U
2017 becomes an equality when U is scheduled in this same row, and (3)
2018 whose dependence latency is zero.
2019
2020 The first and last rows are calculated using the following parameters:
2021 START/END rows - The cycles that begins/ends the traversal on the window;
2022 searching for an empty cycle to schedule U_NODE.
2023 STEP - The direction in which we traverse the window.
2024 II - The initiation interval. */
2025
2026 static void
2027 calculate_must_precede_follow (ddg_node_ptr u_node, int start, int end,
2028 int step, int ii, sbitmap sched_nodes,
2029 sbitmap must_precede, sbitmap must_follow)
2030 {
2031 ddg_edge_ptr e;
2032 int first_cycle_in_window, last_cycle_in_window;
2033
2034 gcc_assert (must_precede && must_follow);
2035
2036 /* Consider the following scheduling window:
2037 {first_cycle_in_window, first_cycle_in_window+1, ...,
2038 last_cycle_in_window}. If step is 1 then the following will be
2039 the order we traverse the window: {start=first_cycle_in_window,
2040 first_cycle_in_window+1, ..., end=last_cycle_in_window+1},
2041 or {start=last_cycle_in_window, last_cycle_in_window-1, ...,
2042 end=first_cycle_in_window-1} if step is -1. */
2043 first_cycle_in_window = (step == 1) ? start : end - step;
2044 last_cycle_in_window = (step == 1) ? end - step : start;
2045
2046 bitmap_clear (must_precede);
2047 bitmap_clear (must_follow);
2048
2049 if (dump_file)
2050 fprintf (dump_file, "\nmust_precede: ");
2051
2052 /* Instead of checking if:
2053 (SMODULO (SCHED_TIME (e->src), ii) == first_row_in_window)
2054 && ((SCHED_TIME (e->src) + e->latency - (e->distance * ii)) ==
2055 first_cycle_in_window)
2056 && e->latency == 0
2057 we use the fact that latency is non-negative:
2058 SCHED_TIME (e->src) - (e->distance * ii) <=
2059 SCHED_TIME (e->src) + e->latency - (e->distance * ii)) <=
2060 first_cycle_in_window
2061 and check only if
2062 SCHED_TIME (e->src) - (e->distance * ii) == first_cycle_in_window */
2063 for (e = u_node->in; e != 0; e = e->next_in)
2064 if (bitmap_bit_p (sched_nodes, e->src->cuid)
2065 && ((SCHED_TIME (e->src->cuid) - (e->distance * ii)) ==
2066 first_cycle_in_window))
2067 {
2068 if (dump_file)
2069 fprintf (dump_file, "%d ", e->src->cuid);
2070
2071 bitmap_set_bit (must_precede, e->src->cuid);
2072 }
2073
2074 if (dump_file)
2075 fprintf (dump_file, "\nmust_follow: ");
2076
2077 /* Instead of checking if:
2078 (SMODULO (SCHED_TIME (e->dest), ii) == last_row_in_window)
2079 && ((SCHED_TIME (e->dest) - e->latency + (e->distance * ii)) ==
2080 last_cycle_in_window)
2081 && e->latency == 0
2082 we use the fact that latency is non-negative:
2083 SCHED_TIME (e->dest) + (e->distance * ii) >=
2084 SCHED_TIME (e->dest) - e->latency + (e->distance * ii)) >=
2085 last_cycle_in_window
2086 and check only if
2087 SCHED_TIME (e->dest) + (e->distance * ii) == last_cycle_in_window */
2088 for (e = u_node->out; e != 0; e = e->next_out)
2089 if (bitmap_bit_p (sched_nodes, e->dest->cuid)
2090 && ((SCHED_TIME (e->dest->cuid) + (e->distance * ii)) ==
2091 last_cycle_in_window))
2092 {
2093 if (dump_file)
2094 fprintf (dump_file, "%d ", e->dest->cuid);
2095
2096 bitmap_set_bit (must_follow, e->dest->cuid);
2097 }
2098
2099 if (dump_file)
2100 fprintf (dump_file, "\n");
2101 }
2102
2103 /* Return 1 if U_NODE can be scheduled in CYCLE. Use the following
2104 parameters to decide if that's possible:
2105 PS - The partial schedule.
2106 U - The serial number of U_NODE.
2107 NUM_SPLITS - The number of row splits made so far.
2108 MUST_PRECEDE - The nodes that must precede U_NODE. (only valid at
2109 the first row of the scheduling window)
2110 MUST_FOLLOW - The nodes that must follow U_NODE. (only valid at the
2111 last row of the scheduling window) */
2112
2113 static bool
2114 try_scheduling_node_in_cycle (partial_schedule_ptr ps,
2115 int u, int cycle, sbitmap sched_nodes,
2116 int *num_splits, sbitmap must_precede,
2117 sbitmap must_follow)
2118 {
2119 ps_insn_ptr psi;
2120 bool success = 0;
2121
2122 verify_partial_schedule (ps, sched_nodes);
2123 psi = ps_add_node_check_conflicts (ps, u, cycle, must_precede, must_follow);
2124 if (psi)
2125 {
2126 SCHED_TIME (u) = cycle;
2127 bitmap_set_bit (sched_nodes, u);
2128 success = 1;
2129 *num_splits = 0;
2130 if (dump_file)
2131 fprintf (dump_file, "Scheduled w/o split in %d\n", cycle);
2132
2133 }
2134
2135 return success;
2136 }
2137
2138 /* This function implements the scheduling algorithm for SMS according to the
2139 above algorithm. */
2140 static partial_schedule_ptr
2141 sms_schedule_by_order (ddg_ptr g, int mii, int maxii, int *nodes_order)
2142 {
2143 int ii = mii;
2144 int i, c, success, num_splits = 0;
2145 int flush_and_start_over = true;
2146 int num_nodes = g->num_nodes;
2147 int start, end, step; /* Place together into one struct? */
2148 sbitmap sched_nodes = sbitmap_alloc (num_nodes);
2149 sbitmap must_precede = sbitmap_alloc (num_nodes);
2150 sbitmap must_follow = sbitmap_alloc (num_nodes);
2151 sbitmap tobe_scheduled = sbitmap_alloc (num_nodes);
2152
2153 partial_schedule_ptr ps = create_partial_schedule (ii, g, DFA_HISTORY);
2154
2155 bitmap_ones (tobe_scheduled);
2156 bitmap_clear (sched_nodes);
2157
2158 while (flush_and_start_over && (ii < maxii))
2159 {
2160
2161 if (dump_file)
2162 fprintf (dump_file, "Starting with ii=%d\n", ii);
2163 flush_and_start_over = false;
2164 bitmap_clear (sched_nodes);
2165
2166 for (i = 0; i < num_nodes; i++)
2167 {
2168 int u = nodes_order[i];
2169 ddg_node_ptr u_node = &ps->g->nodes[u];
2170 rtx insn = u_node->insn;
2171
2172 if (!NONDEBUG_INSN_P (insn))
2173 {
2174 bitmap_clear_bit (tobe_scheduled, u);
2175 continue;
2176 }
2177
2178 if (bitmap_bit_p (sched_nodes, u))
2179 continue;
2180
2181 /* Try to get non-empty scheduling window. */
2182 success = 0;
2183 if (get_sched_window (ps, u_node, sched_nodes, ii, &start,
2184 &step, &end) == 0)
2185 {
2186 if (dump_file)
2187 fprintf (dump_file, "\nTrying to schedule node %d "
2188 "INSN = %d in (%d .. %d) step %d\n", u, (INSN_UID
2189 (g->nodes[u].insn)), start, end, step);
2190
2191 gcc_assert ((step > 0 && start < end)
2192 || (step < 0 && start > end));
2193
2194 calculate_must_precede_follow (u_node, start, end, step, ii,
2195 sched_nodes, must_precede,
2196 must_follow);
2197
2198 for (c = start; c != end; c += step)
2199 {
2200 sbitmap tmp_precede, tmp_follow;
2201
2202 set_must_precede_follow (&tmp_follow, must_follow,
2203 &tmp_precede, must_precede,
2204 c, start, end, step);
2205 success =
2206 try_scheduling_node_in_cycle (ps, u, c,
2207 sched_nodes,
2208 &num_splits, tmp_precede,
2209 tmp_follow);
2210 if (success)
2211 break;
2212 }
2213
2214 verify_partial_schedule (ps, sched_nodes);
2215 }
2216 if (!success)
2217 {
2218 int split_row;
2219
2220 if (ii++ == maxii)
2221 break;
2222
2223 if (num_splits >= MAX_SPLIT_NUM)
2224 {
2225 num_splits = 0;
2226 flush_and_start_over = true;
2227 verify_partial_schedule (ps, sched_nodes);
2228 reset_partial_schedule (ps, ii);
2229 verify_partial_schedule (ps, sched_nodes);
2230 break;
2231 }
2232
2233 num_splits++;
2234 /* The scheduling window is exclusive of 'end'
2235 whereas compute_split_window() expects an inclusive,
2236 ordered range. */
2237 if (step == 1)
2238 split_row = compute_split_row (sched_nodes, start, end - 1,
2239 ps->ii, u_node);
2240 else
2241 split_row = compute_split_row (sched_nodes, end + 1, start,
2242 ps->ii, u_node);
2243
2244 ps_insert_empty_row (ps, split_row, sched_nodes);
2245 i--; /* Go back and retry node i. */
2246
2247 if (dump_file)
2248 fprintf (dump_file, "num_splits=%d\n", num_splits);
2249 }
2250
2251 /* ??? If (success), check register pressure estimates. */
2252 } /* Continue with next node. */
2253 } /* While flush_and_start_over. */
2254 if (ii >= maxii)
2255 {
2256 free_partial_schedule (ps);
2257 ps = NULL;
2258 }
2259 else
2260 gcc_assert (bitmap_equal_p (tobe_scheduled, sched_nodes));
2261
2262 sbitmap_free (sched_nodes);
2263 sbitmap_free (must_precede);
2264 sbitmap_free (must_follow);
2265 sbitmap_free (tobe_scheduled);
2266
2267 return ps;
2268 }
2269
2270 /* This function inserts a new empty row into PS at the position
2271 according to SPLITROW, keeping all already scheduled instructions
2272 intact and updating their SCHED_TIME and cycle accordingly. */
2273 static void
2274 ps_insert_empty_row (partial_schedule_ptr ps, int split_row,
2275 sbitmap sched_nodes)
2276 {
2277 ps_insn_ptr crr_insn;
2278 ps_insn_ptr *rows_new;
2279 int ii = ps->ii;
2280 int new_ii = ii + 1;
2281 int row;
2282 int *rows_length_new;
2283
2284 verify_partial_schedule (ps, sched_nodes);
2285
2286 /* We normalize sched_time and rotate ps to have only non-negative sched
2287 times, for simplicity of updating cycles after inserting new row. */
2288 split_row -= ps->min_cycle;
2289 split_row = SMODULO (split_row, ii);
2290 if (dump_file)
2291 fprintf (dump_file, "split_row=%d\n", split_row);
2292
2293 reset_sched_times (ps, PS_MIN_CYCLE (ps));
2294 rotate_partial_schedule (ps, PS_MIN_CYCLE (ps));
2295
2296 rows_new = (ps_insn_ptr *) xcalloc (new_ii, sizeof (ps_insn_ptr));
2297 rows_length_new = (int *) xcalloc (new_ii, sizeof (int));
2298 for (row = 0; row < split_row; row++)
2299 {
2300 rows_new[row] = ps->rows[row];
2301 rows_length_new[row] = ps->rows_length[row];
2302 ps->rows[row] = NULL;
2303 for (crr_insn = rows_new[row];
2304 crr_insn; crr_insn = crr_insn->next_in_row)
2305 {
2306 int u = crr_insn->id;
2307 int new_time = SCHED_TIME (u) + (SCHED_TIME (u) / ii);
2308
2309 SCHED_TIME (u) = new_time;
2310 crr_insn->cycle = new_time;
2311 SCHED_ROW (u) = new_time % new_ii;
2312 SCHED_STAGE (u) = new_time / new_ii;
2313 }
2314
2315 }
2316
2317 rows_new[split_row] = NULL;
2318
2319 for (row = split_row; row < ii; row++)
2320 {
2321 rows_new[row + 1] = ps->rows[row];
2322 rows_length_new[row + 1] = ps->rows_length[row];
2323 ps->rows[row] = NULL;
2324 for (crr_insn = rows_new[row + 1];
2325 crr_insn; crr_insn = crr_insn->next_in_row)
2326 {
2327 int u = crr_insn->id;
2328 int new_time = SCHED_TIME (u) + (SCHED_TIME (u) / ii) + 1;
2329
2330 SCHED_TIME (u) = new_time;
2331 crr_insn->cycle = new_time;
2332 SCHED_ROW (u) = new_time % new_ii;
2333 SCHED_STAGE (u) = new_time / new_ii;
2334 }
2335 }
2336
2337 /* Updating ps. */
2338 ps->min_cycle = ps->min_cycle + ps->min_cycle / ii
2339 + (SMODULO (ps->min_cycle, ii) >= split_row ? 1 : 0);
2340 ps->max_cycle = ps->max_cycle + ps->max_cycle / ii
2341 + (SMODULO (ps->max_cycle, ii) >= split_row ? 1 : 0);
2342 free (ps->rows);
2343 ps->rows = rows_new;
2344 free (ps->rows_length);
2345 ps->rows_length = rows_length_new;
2346 ps->ii = new_ii;
2347 gcc_assert (ps->min_cycle >= 0);
2348
2349 verify_partial_schedule (ps, sched_nodes);
2350
2351 if (dump_file)
2352 fprintf (dump_file, "min_cycle=%d, max_cycle=%d\n", ps->min_cycle,
2353 ps->max_cycle);
2354 }
2355
2356 /* Given U_NODE which is the node that failed to be scheduled; LOW and
2357 UP which are the boundaries of it's scheduling window; compute using
2358 SCHED_NODES and II a row in the partial schedule that can be split
2359 which will separate a critical predecessor from a critical successor
2360 thereby expanding the window, and return it. */
2361 static int
2362 compute_split_row (sbitmap sched_nodes, int low, int up, int ii,
2363 ddg_node_ptr u_node)
2364 {
2365 ddg_edge_ptr e;
2366 int lower = INT_MIN, upper = INT_MAX;
2367 int crit_pred = -1;
2368 int crit_succ = -1;
2369 int crit_cycle;
2370
2371 for (e = u_node->in; e != 0; e = e->next_in)
2372 {
2373 int v = e->src->cuid;
2374
2375 if (bitmap_bit_p (sched_nodes, v)
2376 && (low == SCHED_TIME (v) + e->latency - (e->distance * ii)))
2377 if (SCHED_TIME (v) > lower)
2378 {
2379 crit_pred = v;
2380 lower = SCHED_TIME (v);
2381 }
2382 }
2383
2384 if (crit_pred >= 0)
2385 {
2386 crit_cycle = SCHED_TIME (crit_pred) + 1;
2387 return SMODULO (crit_cycle, ii);
2388 }
2389
2390 for (e = u_node->out; e != 0; e = e->next_out)
2391 {
2392 int v = e->dest->cuid;
2393
2394 if (bitmap_bit_p (sched_nodes, v)
2395 && (up == SCHED_TIME (v) - e->latency + (e->distance * ii)))
2396 if (SCHED_TIME (v) < upper)
2397 {
2398 crit_succ = v;
2399 upper = SCHED_TIME (v);
2400 }
2401 }
2402
2403 if (crit_succ >= 0)
2404 {
2405 crit_cycle = SCHED_TIME (crit_succ);
2406 return SMODULO (crit_cycle, ii);
2407 }
2408
2409 if (dump_file)
2410 fprintf (dump_file, "Both crit_pred and crit_succ are NULL\n");
2411
2412 return SMODULO ((low + up + 1) / 2, ii);
2413 }
2414
2415 static void
2416 verify_partial_schedule (partial_schedule_ptr ps, sbitmap sched_nodes)
2417 {
2418 int row;
2419 ps_insn_ptr crr_insn;
2420
2421 for (row = 0; row < ps->ii; row++)
2422 {
2423 int length = 0;
2424
2425 for (crr_insn = ps->rows[row]; crr_insn; crr_insn = crr_insn->next_in_row)
2426 {
2427 int u = crr_insn->id;
2428
2429 length++;
2430 gcc_assert (bitmap_bit_p (sched_nodes, u));
2431 /* ??? Test also that all nodes of sched_nodes are in ps, perhaps by
2432 popcount (sched_nodes) == number of insns in ps. */
2433 gcc_assert (SCHED_TIME (u) >= ps->min_cycle);
2434 gcc_assert (SCHED_TIME (u) <= ps->max_cycle);
2435 }
2436
2437 gcc_assert (ps->rows_length[row] == length);
2438 }
2439 }
2440
2441 \f
2442 /* This page implements the algorithm for ordering the nodes of a DDG
2443 for modulo scheduling, activated through the
2444 "int sms_order_nodes (ddg_ptr, int mii, int * result)" API. */
2445
2446 #define ORDER_PARAMS(x) ((struct node_order_params *) (x)->aux.info)
2447 #define ASAP(x) (ORDER_PARAMS ((x))->asap)
2448 #define ALAP(x) (ORDER_PARAMS ((x))->alap)
2449 #define HEIGHT(x) (ORDER_PARAMS ((x))->height)
2450 #define MOB(x) (ALAP ((x)) - ASAP ((x)))
2451 #define DEPTH(x) (ASAP ((x)))
2452
2453 typedef struct node_order_params * nopa;
2454
2455 static void order_nodes_of_sccs (ddg_all_sccs_ptr, int * result);
2456 static int order_nodes_in_scc (ddg_ptr, sbitmap, sbitmap, int*, int);
2457 static nopa calculate_order_params (ddg_ptr, int, int *);
2458 static int find_max_asap (ddg_ptr, sbitmap);
2459 static int find_max_hv_min_mob (ddg_ptr, sbitmap);
2460 static int find_max_dv_min_mob (ddg_ptr, sbitmap);
2461
2462 enum sms_direction {BOTTOMUP, TOPDOWN};
2463
2464 struct node_order_params
2465 {
2466 int asap;
2467 int alap;
2468 int height;
2469 };
2470
2471 /* Check if NODE_ORDER contains a permutation of 0 .. NUM_NODES-1. */
2472 static void
2473 check_nodes_order (int *node_order, int num_nodes)
2474 {
2475 int i;
2476 sbitmap tmp = sbitmap_alloc (num_nodes);
2477
2478 bitmap_clear (tmp);
2479
2480 if (dump_file)
2481 fprintf (dump_file, "SMS final nodes order: \n");
2482
2483 for (i = 0; i < num_nodes; i++)
2484 {
2485 int u = node_order[i];
2486
2487 if (dump_file)
2488 fprintf (dump_file, "%d ", u);
2489 gcc_assert (u < num_nodes && u >= 0 && !bitmap_bit_p (tmp, u));
2490
2491 bitmap_set_bit (tmp, u);
2492 }
2493
2494 if (dump_file)
2495 fprintf (dump_file, "\n");
2496
2497 sbitmap_free (tmp);
2498 }
2499
2500 /* Order the nodes of G for scheduling and pass the result in
2501 NODE_ORDER. Also set aux.count of each node to ASAP.
2502 Put maximal ASAP to PMAX_ASAP. Return the recMII for the given DDG. */
2503 static int
2504 sms_order_nodes (ddg_ptr g, int mii, int * node_order, int *pmax_asap)
2505 {
2506 int i;
2507 int rec_mii = 0;
2508 ddg_all_sccs_ptr sccs = create_ddg_all_sccs (g);
2509
2510 nopa nops = calculate_order_params (g, mii, pmax_asap);
2511
2512 if (dump_file)
2513 print_sccs (dump_file, sccs, g);
2514
2515 order_nodes_of_sccs (sccs, node_order);
2516
2517 if (sccs->num_sccs > 0)
2518 /* First SCC has the largest recurrence_length. */
2519 rec_mii = sccs->sccs[0]->recurrence_length;
2520
2521 /* Save ASAP before destroying node_order_params. */
2522 for (i = 0; i < g->num_nodes; i++)
2523 {
2524 ddg_node_ptr v = &g->nodes[i];
2525 v->aux.count = ASAP (v);
2526 }
2527
2528 free (nops);
2529 free_ddg_all_sccs (sccs);
2530 check_nodes_order (node_order, g->num_nodes);
2531
2532 return rec_mii;
2533 }
2534
2535 static void
2536 order_nodes_of_sccs (ddg_all_sccs_ptr all_sccs, int * node_order)
2537 {
2538 int i, pos = 0;
2539 ddg_ptr g = all_sccs->ddg;
2540 int num_nodes = g->num_nodes;
2541 sbitmap prev_sccs = sbitmap_alloc (num_nodes);
2542 sbitmap on_path = sbitmap_alloc (num_nodes);
2543 sbitmap tmp = sbitmap_alloc (num_nodes);
2544 sbitmap ones = sbitmap_alloc (num_nodes);
2545
2546 bitmap_clear (prev_sccs);
2547 bitmap_ones (ones);
2548
2549 /* Perform the node ordering starting from the SCC with the highest recMII.
2550 For each SCC order the nodes according to their ASAP/ALAP/HEIGHT etc. */
2551 for (i = 0; i < all_sccs->num_sccs; i++)
2552 {
2553 ddg_scc_ptr scc = all_sccs->sccs[i];
2554
2555 /* Add nodes on paths from previous SCCs to the current SCC. */
2556 find_nodes_on_paths (on_path, g, prev_sccs, scc->nodes);
2557 bitmap_ior (tmp, scc->nodes, on_path);
2558
2559 /* Add nodes on paths from the current SCC to previous SCCs. */
2560 find_nodes_on_paths (on_path, g, scc->nodes, prev_sccs);
2561 bitmap_ior (tmp, tmp, on_path);
2562
2563 /* Remove nodes of previous SCCs from current extended SCC. */
2564 bitmap_and_compl (tmp, tmp, prev_sccs);
2565
2566 pos = order_nodes_in_scc (g, prev_sccs, tmp, node_order, pos);
2567 /* Above call to order_nodes_in_scc updated prev_sccs |= tmp. */
2568 }
2569
2570 /* Handle the remaining nodes that do not belong to any scc. Each call
2571 to order_nodes_in_scc handles a single connected component. */
2572 while (pos < g->num_nodes)
2573 {
2574 bitmap_and_compl (tmp, ones, prev_sccs);
2575 pos = order_nodes_in_scc (g, prev_sccs, tmp, node_order, pos);
2576 }
2577 sbitmap_free (prev_sccs);
2578 sbitmap_free (on_path);
2579 sbitmap_free (tmp);
2580 sbitmap_free (ones);
2581 }
2582
2583 /* MII is needed if we consider backarcs (that do not close recursive cycles). */
2584 static struct node_order_params *
2585 calculate_order_params (ddg_ptr g, int mii ATTRIBUTE_UNUSED, int *pmax_asap)
2586 {
2587 int u;
2588 int max_asap;
2589 int num_nodes = g->num_nodes;
2590 ddg_edge_ptr e;
2591 /* Allocate a place to hold ordering params for each node in the DDG. */
2592 nopa node_order_params_arr;
2593
2594 /* Initialize of ASAP/ALAP/HEIGHT to zero. */
2595 node_order_params_arr = (nopa) xcalloc (num_nodes,
2596 sizeof (struct node_order_params));
2597
2598 /* Set the aux pointer of each node to point to its order_params structure. */
2599 for (u = 0; u < num_nodes; u++)
2600 g->nodes[u].aux.info = &node_order_params_arr[u];
2601
2602 /* Disregarding a backarc from each recursive cycle to obtain a DAG,
2603 calculate ASAP, ALAP, mobility, distance, and height for each node
2604 in the dependence (direct acyclic) graph. */
2605
2606 /* We assume that the nodes in the array are in topological order. */
2607
2608 max_asap = 0;
2609 for (u = 0; u < num_nodes; u++)
2610 {
2611 ddg_node_ptr u_node = &g->nodes[u];
2612
2613 ASAP (u_node) = 0;
2614 for (e = u_node->in; e; e = e->next_in)
2615 if (e->distance == 0)
2616 ASAP (u_node) = MAX (ASAP (u_node),
2617 ASAP (e->src) + e->latency);
2618 max_asap = MAX (max_asap, ASAP (u_node));
2619 }
2620
2621 for (u = num_nodes - 1; u > -1; u--)
2622 {
2623 ddg_node_ptr u_node = &g->nodes[u];
2624
2625 ALAP (u_node) = max_asap;
2626 HEIGHT (u_node) = 0;
2627 for (e = u_node->out; e; e = e->next_out)
2628 if (e->distance == 0)
2629 {
2630 ALAP (u_node) = MIN (ALAP (u_node),
2631 ALAP (e->dest) - e->latency);
2632 HEIGHT (u_node) = MAX (HEIGHT (u_node),
2633 HEIGHT (e->dest) + e->latency);
2634 }
2635 }
2636 if (dump_file)
2637 {
2638 fprintf (dump_file, "\nOrder params\n");
2639 for (u = 0; u < num_nodes; u++)
2640 {
2641 ddg_node_ptr u_node = &g->nodes[u];
2642
2643 fprintf (dump_file, "node %d, ASAP: %d, ALAP: %d, HEIGHT: %d\n", u,
2644 ASAP (u_node), ALAP (u_node), HEIGHT (u_node));
2645 }
2646 }
2647
2648 *pmax_asap = max_asap;
2649 return node_order_params_arr;
2650 }
2651
2652 static int
2653 find_max_asap (ddg_ptr g, sbitmap nodes)
2654 {
2655 unsigned int u = 0;
2656 int max_asap = -1;
2657 int result = -1;
2658 sbitmap_iterator sbi;
2659
2660 EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
2661 {
2662 ddg_node_ptr u_node = &g->nodes[u];
2663
2664 if (max_asap < ASAP (u_node))
2665 {
2666 max_asap = ASAP (u_node);
2667 result = u;
2668 }
2669 }
2670 return result;
2671 }
2672
2673 static int
2674 find_max_hv_min_mob (ddg_ptr g, sbitmap nodes)
2675 {
2676 unsigned int u = 0;
2677 int max_hv = -1;
2678 int min_mob = INT_MAX;
2679 int result = -1;
2680 sbitmap_iterator sbi;
2681
2682 EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
2683 {
2684 ddg_node_ptr u_node = &g->nodes[u];
2685
2686 if (max_hv < HEIGHT (u_node))
2687 {
2688 max_hv = HEIGHT (u_node);
2689 min_mob = MOB (u_node);
2690 result = u;
2691 }
2692 else if ((max_hv == HEIGHT (u_node))
2693 && (min_mob > MOB (u_node)))
2694 {
2695 min_mob = MOB (u_node);
2696 result = u;
2697 }
2698 }
2699 return result;
2700 }
2701
2702 static int
2703 find_max_dv_min_mob (ddg_ptr g, sbitmap nodes)
2704 {
2705 unsigned int u = 0;
2706 int max_dv = -1;
2707 int min_mob = INT_MAX;
2708 int result = -1;
2709 sbitmap_iterator sbi;
2710
2711 EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
2712 {
2713 ddg_node_ptr u_node = &g->nodes[u];
2714
2715 if (max_dv < DEPTH (u_node))
2716 {
2717 max_dv = DEPTH (u_node);
2718 min_mob = MOB (u_node);
2719 result = u;
2720 }
2721 else if ((max_dv == DEPTH (u_node))
2722 && (min_mob > MOB (u_node)))
2723 {
2724 min_mob = MOB (u_node);
2725 result = u;
2726 }
2727 }
2728 return result;
2729 }
2730
2731 /* Places the nodes of SCC into the NODE_ORDER array starting
2732 at position POS, according to the SMS ordering algorithm.
2733 NODES_ORDERED (in&out parameter) holds the bitset of all nodes in
2734 the NODE_ORDER array, starting from position zero. */
2735 static int
2736 order_nodes_in_scc (ddg_ptr g, sbitmap nodes_ordered, sbitmap scc,
2737 int * node_order, int pos)
2738 {
2739 enum sms_direction dir;
2740 int num_nodes = g->num_nodes;
2741 sbitmap workset = sbitmap_alloc (num_nodes);
2742 sbitmap tmp = sbitmap_alloc (num_nodes);
2743 sbitmap zero_bitmap = sbitmap_alloc (num_nodes);
2744 sbitmap predecessors = sbitmap_alloc (num_nodes);
2745 sbitmap successors = sbitmap_alloc (num_nodes);
2746
2747 bitmap_clear (predecessors);
2748 find_predecessors (predecessors, g, nodes_ordered);
2749
2750 bitmap_clear (successors);
2751 find_successors (successors, g, nodes_ordered);
2752
2753 bitmap_clear (tmp);
2754 if (bitmap_and (tmp, predecessors, scc))
2755 {
2756 bitmap_copy (workset, tmp);
2757 dir = BOTTOMUP;
2758 }
2759 else if (bitmap_and (tmp, successors, scc))
2760 {
2761 bitmap_copy (workset, tmp);
2762 dir = TOPDOWN;
2763 }
2764 else
2765 {
2766 int u;
2767
2768 bitmap_clear (workset);
2769 if ((u = find_max_asap (g, scc)) >= 0)
2770 bitmap_set_bit (workset, u);
2771 dir = BOTTOMUP;
2772 }
2773
2774 bitmap_clear (zero_bitmap);
2775 while (!bitmap_equal_p (workset, zero_bitmap))
2776 {
2777 int v;
2778 ddg_node_ptr v_node;
2779 sbitmap v_node_preds;
2780 sbitmap v_node_succs;
2781
2782 if (dir == TOPDOWN)
2783 {
2784 while (!bitmap_equal_p (workset, zero_bitmap))
2785 {
2786 v = find_max_hv_min_mob (g, workset);
2787 v_node = &g->nodes[v];
2788 node_order[pos++] = v;
2789 v_node_succs = NODE_SUCCESSORS (v_node);
2790 bitmap_and (tmp, v_node_succs, scc);
2791
2792 /* Don't consider the already ordered successors again. */
2793 bitmap_and_compl (tmp, tmp, nodes_ordered);
2794 bitmap_ior (workset, workset, tmp);
2795 bitmap_clear_bit (workset, v);
2796 bitmap_set_bit (nodes_ordered, v);
2797 }
2798 dir = BOTTOMUP;
2799 bitmap_clear (predecessors);
2800 find_predecessors (predecessors, g, nodes_ordered);
2801 bitmap_and (workset, predecessors, scc);
2802 }
2803 else
2804 {
2805 while (!bitmap_equal_p (workset, zero_bitmap))
2806 {
2807 v = find_max_dv_min_mob (g, workset);
2808 v_node = &g->nodes[v];
2809 node_order[pos++] = v;
2810 v_node_preds = NODE_PREDECESSORS (v_node);
2811 bitmap_and (tmp, v_node_preds, scc);
2812
2813 /* Don't consider the already ordered predecessors again. */
2814 bitmap_and_compl (tmp, tmp, nodes_ordered);
2815 bitmap_ior (workset, workset, tmp);
2816 bitmap_clear_bit (workset, v);
2817 bitmap_set_bit (nodes_ordered, v);
2818 }
2819 dir = TOPDOWN;
2820 bitmap_clear (successors);
2821 find_successors (successors, g, nodes_ordered);
2822 bitmap_and (workset, successors, scc);
2823 }
2824 }
2825 sbitmap_free (tmp);
2826 sbitmap_free (workset);
2827 sbitmap_free (zero_bitmap);
2828 sbitmap_free (predecessors);
2829 sbitmap_free (successors);
2830 return pos;
2831 }
2832
2833 \f
2834 /* This page contains functions for manipulating partial-schedules during
2835 modulo scheduling. */
2836
2837 /* Create a partial schedule and allocate a memory to hold II rows. */
2838
2839 static partial_schedule_ptr
2840 create_partial_schedule (int ii, ddg_ptr g, int history)
2841 {
2842 partial_schedule_ptr ps = XNEW (struct partial_schedule);
2843 ps->rows = (ps_insn_ptr *) xcalloc (ii, sizeof (ps_insn_ptr));
2844 ps->rows_length = (int *) xcalloc (ii, sizeof (int));
2845 ps->reg_moves.create (0);
2846 ps->ii = ii;
2847 ps->history = history;
2848 ps->min_cycle = INT_MAX;
2849 ps->max_cycle = INT_MIN;
2850 ps->g = g;
2851
2852 return ps;
2853 }
2854
2855 /* Free the PS_INSNs in rows array of the given partial schedule.
2856 ??? Consider caching the PS_INSN's. */
2857 static void
2858 free_ps_insns (partial_schedule_ptr ps)
2859 {
2860 int i;
2861
2862 for (i = 0; i < ps->ii; i++)
2863 {
2864 while (ps->rows[i])
2865 {
2866 ps_insn_ptr ps_insn = ps->rows[i]->next_in_row;
2867
2868 free (ps->rows[i]);
2869 ps->rows[i] = ps_insn;
2870 }
2871 ps->rows[i] = NULL;
2872 }
2873 }
2874
2875 /* Free all the memory allocated to the partial schedule. */
2876
2877 static void
2878 free_partial_schedule (partial_schedule_ptr ps)
2879 {
2880 ps_reg_move_info *move;
2881 unsigned int i;
2882
2883 if (!ps)
2884 return;
2885
2886 FOR_EACH_VEC_ELT (ps->reg_moves, i, move)
2887 sbitmap_free (move->uses);
2888 ps->reg_moves.release ();
2889
2890 free_ps_insns (ps);
2891 free (ps->rows);
2892 free (ps->rows_length);
2893 free (ps);
2894 }
2895
2896 /* Clear the rows array with its PS_INSNs, and create a new one with
2897 NEW_II rows. */
2898
2899 static void
2900 reset_partial_schedule (partial_schedule_ptr ps, int new_ii)
2901 {
2902 if (!ps)
2903 return;
2904 free_ps_insns (ps);
2905 if (new_ii == ps->ii)
2906 return;
2907 ps->rows = (ps_insn_ptr *) xrealloc (ps->rows, new_ii
2908 * sizeof (ps_insn_ptr));
2909 memset (ps->rows, 0, new_ii * sizeof (ps_insn_ptr));
2910 ps->rows_length = (int *) xrealloc (ps->rows_length, new_ii * sizeof (int));
2911 memset (ps->rows_length, 0, new_ii * sizeof (int));
2912 ps->ii = new_ii;
2913 ps->min_cycle = INT_MAX;
2914 ps->max_cycle = INT_MIN;
2915 }
2916
2917 /* Prints the partial schedule as an ii rows array, for each rows
2918 print the ids of the insns in it. */
2919 void
2920 print_partial_schedule (partial_schedule_ptr ps, FILE *dump)
2921 {
2922 int i;
2923
2924 for (i = 0; i < ps->ii; i++)
2925 {
2926 ps_insn_ptr ps_i = ps->rows[i];
2927
2928 fprintf (dump, "\n[ROW %d ]: ", i);
2929 while (ps_i)
2930 {
2931 rtx insn = ps_rtl_insn (ps, ps_i->id);
2932
2933 if (JUMP_P (insn))
2934 fprintf (dump, "%d (branch), ", INSN_UID (insn));
2935 else
2936 fprintf (dump, "%d, ", INSN_UID (insn));
2937
2938 ps_i = ps_i->next_in_row;
2939 }
2940 }
2941 }
2942
2943 /* Creates an object of PS_INSN and initializes it to the given parameters. */
2944 static ps_insn_ptr
2945 create_ps_insn (int id, int cycle)
2946 {
2947 ps_insn_ptr ps_i = XNEW (struct ps_insn);
2948
2949 ps_i->id = id;
2950 ps_i->next_in_row = NULL;
2951 ps_i->prev_in_row = NULL;
2952 ps_i->cycle = cycle;
2953
2954 return ps_i;
2955 }
2956
2957
2958 /* Removes the given PS_INSN from the partial schedule. */
2959 static void
2960 remove_node_from_ps (partial_schedule_ptr ps, ps_insn_ptr ps_i)
2961 {
2962 int row;
2963
2964 gcc_assert (ps && ps_i);
2965
2966 row = SMODULO (ps_i->cycle, ps->ii);
2967 if (! ps_i->prev_in_row)
2968 {
2969 gcc_assert (ps_i == ps->rows[row]);
2970 ps->rows[row] = ps_i->next_in_row;
2971 if (ps->rows[row])
2972 ps->rows[row]->prev_in_row = NULL;
2973 }
2974 else
2975 {
2976 ps_i->prev_in_row->next_in_row = ps_i->next_in_row;
2977 if (ps_i->next_in_row)
2978 ps_i->next_in_row->prev_in_row = ps_i->prev_in_row;
2979 }
2980
2981 ps->rows_length[row] -= 1;
2982 free (ps_i);
2983 return;
2984 }
2985
2986 /* Unlike what literature describes for modulo scheduling (which focuses
2987 on VLIW machines) the order of the instructions inside a cycle is
2988 important. Given the bitmaps MUST_FOLLOW and MUST_PRECEDE we know
2989 where the current instruction should go relative to the already
2990 scheduled instructions in the given cycle. Go over these
2991 instructions and find the first possible column to put it in. */
2992 static bool
2993 ps_insn_find_column (partial_schedule_ptr ps, ps_insn_ptr ps_i,
2994 sbitmap must_precede, sbitmap must_follow)
2995 {
2996 ps_insn_ptr next_ps_i;
2997 ps_insn_ptr first_must_follow = NULL;
2998 ps_insn_ptr last_must_precede = NULL;
2999 ps_insn_ptr last_in_row = NULL;
3000 int row;
3001
3002 if (! ps_i)
3003 return false;
3004
3005 row = SMODULO (ps_i->cycle, ps->ii);
3006
3007 /* Find the first must follow and the last must precede
3008 and insert the node immediately after the must precede
3009 but make sure that it there is no must follow after it. */
3010 for (next_ps_i = ps->rows[row];
3011 next_ps_i;
3012 next_ps_i = next_ps_i->next_in_row)
3013 {
3014 if (must_follow
3015 && bitmap_bit_p (must_follow, next_ps_i->id)
3016 && ! first_must_follow)
3017 first_must_follow = next_ps_i;
3018 if (must_precede && bitmap_bit_p (must_precede, next_ps_i->id))
3019 {
3020 /* If we have already met a node that must follow, then
3021 there is no possible column. */
3022 if (first_must_follow)
3023 return false;
3024 else
3025 last_must_precede = next_ps_i;
3026 }
3027 /* The closing branch must be the last in the row. */
3028 if (must_precede
3029 && bitmap_bit_p (must_precede, next_ps_i->id)
3030 && JUMP_P (ps_rtl_insn (ps, next_ps_i->id)))
3031 return false;
3032
3033 last_in_row = next_ps_i;
3034 }
3035
3036 /* The closing branch is scheduled as well. Make sure there is no
3037 dependent instruction after it as the branch should be the last
3038 instruction in the row. */
3039 if (JUMP_P (ps_rtl_insn (ps, ps_i->id)))
3040 {
3041 if (first_must_follow)
3042 return false;
3043 if (last_in_row)
3044 {
3045 /* Make the branch the last in the row. New instructions
3046 will be inserted at the beginning of the row or after the
3047 last must_precede instruction thus the branch is guaranteed
3048 to remain the last instruction in the row. */
3049 last_in_row->next_in_row = ps_i;
3050 ps_i->prev_in_row = last_in_row;
3051 ps_i->next_in_row = NULL;
3052 }
3053 else
3054 ps->rows[row] = ps_i;
3055 return true;
3056 }
3057
3058 /* Now insert the node after INSERT_AFTER_PSI. */
3059
3060 if (! last_must_precede)
3061 {
3062 ps_i->next_in_row = ps->rows[row];
3063 ps_i->prev_in_row = NULL;
3064 if (ps_i->next_in_row)
3065 ps_i->next_in_row->prev_in_row = ps_i;
3066 ps->rows[row] = ps_i;
3067 }
3068 else
3069 {
3070 ps_i->next_in_row = last_must_precede->next_in_row;
3071 last_must_precede->next_in_row = ps_i;
3072 ps_i->prev_in_row = last_must_precede;
3073 if (ps_i->next_in_row)
3074 ps_i->next_in_row->prev_in_row = ps_i;
3075 }
3076
3077 return true;
3078 }
3079
3080 /* Advances the PS_INSN one column in its current row; returns false
3081 in failure and true in success. Bit N is set in MUST_FOLLOW if
3082 the node with cuid N must be come after the node pointed to by
3083 PS_I when scheduled in the same cycle. */
3084 static int
3085 ps_insn_advance_column (partial_schedule_ptr ps, ps_insn_ptr ps_i,
3086 sbitmap must_follow)
3087 {
3088 ps_insn_ptr prev, next;
3089 int row;
3090
3091 if (!ps || !ps_i)
3092 return false;
3093
3094 row = SMODULO (ps_i->cycle, ps->ii);
3095
3096 if (! ps_i->next_in_row)
3097 return false;
3098
3099 /* Check if next_in_row is dependent on ps_i, both having same sched
3100 times (typically ANTI_DEP). If so, ps_i cannot skip over it. */
3101 if (must_follow && bitmap_bit_p (must_follow, ps_i->next_in_row->id))
3102 return false;
3103
3104 /* Advance PS_I over its next_in_row in the doubly linked list. */
3105 prev = ps_i->prev_in_row;
3106 next = ps_i->next_in_row;
3107
3108 if (ps_i == ps->rows[row])
3109 ps->rows[row] = next;
3110
3111 ps_i->next_in_row = next->next_in_row;
3112
3113 if (next->next_in_row)
3114 next->next_in_row->prev_in_row = ps_i;
3115
3116 next->next_in_row = ps_i;
3117 ps_i->prev_in_row = next;
3118
3119 next->prev_in_row = prev;
3120 if (prev)
3121 prev->next_in_row = next;
3122
3123 return true;
3124 }
3125
3126 /* Inserts a DDG_NODE to the given partial schedule at the given cycle.
3127 Returns 0 if this is not possible and a PS_INSN otherwise. Bit N is
3128 set in MUST_PRECEDE/MUST_FOLLOW if the node with cuid N must be come
3129 before/after (respectively) the node pointed to by PS_I when scheduled
3130 in the same cycle. */
3131 static ps_insn_ptr
3132 add_node_to_ps (partial_schedule_ptr ps, int id, int cycle,
3133 sbitmap must_precede, sbitmap must_follow)
3134 {
3135 ps_insn_ptr ps_i;
3136 int row = SMODULO (cycle, ps->ii);
3137
3138 if (ps->rows_length[row] >= issue_rate)
3139 return NULL;
3140
3141 ps_i = create_ps_insn (id, cycle);
3142
3143 /* Finds and inserts PS_I according to MUST_FOLLOW and
3144 MUST_PRECEDE. */
3145 if (! ps_insn_find_column (ps, ps_i, must_precede, must_follow))
3146 {
3147 free (ps_i);
3148 return NULL;
3149 }
3150
3151 ps->rows_length[row] += 1;
3152 return ps_i;
3153 }
3154
3155 /* Advance time one cycle. Assumes DFA is being used. */
3156 static void
3157 advance_one_cycle (void)
3158 {
3159 if (targetm.sched.dfa_pre_cycle_insn)
3160 state_transition (curr_state,
3161 targetm.sched.dfa_pre_cycle_insn ());
3162
3163 state_transition (curr_state, NULL);
3164
3165 if (targetm.sched.dfa_post_cycle_insn)
3166 state_transition (curr_state,
3167 targetm.sched.dfa_post_cycle_insn ());
3168 }
3169
3170
3171
3172 /* Checks if PS has resource conflicts according to DFA, starting from
3173 FROM cycle to TO cycle; returns true if there are conflicts and false
3174 if there are no conflicts. Assumes DFA is being used. */
3175 static int
3176 ps_has_conflicts (partial_schedule_ptr ps, int from, int to)
3177 {
3178 int cycle;
3179
3180 state_reset (curr_state);
3181
3182 for (cycle = from; cycle <= to; cycle++)
3183 {
3184 ps_insn_ptr crr_insn;
3185 /* Holds the remaining issue slots in the current row. */
3186 int can_issue_more = issue_rate;
3187
3188 /* Walk through the DFA for the current row. */
3189 for (crr_insn = ps->rows[SMODULO (cycle, ps->ii)];
3190 crr_insn;
3191 crr_insn = crr_insn->next_in_row)
3192 {
3193 rtx insn = ps_rtl_insn (ps, crr_insn->id);
3194
3195 if (!NONDEBUG_INSN_P (insn))
3196 continue;
3197
3198 /* Check if there is room for the current insn. */
3199 if (!can_issue_more || state_dead_lock_p (curr_state))
3200 return true;
3201
3202 /* Update the DFA state and return with failure if the DFA found
3203 resource conflicts. */
3204 if (state_transition (curr_state, insn) >= 0)
3205 return true;
3206
3207 if (targetm.sched.variable_issue)
3208 can_issue_more =
3209 targetm.sched.variable_issue (sched_dump, sched_verbose,
3210 insn, can_issue_more);
3211 /* A naked CLOBBER or USE generates no instruction, so don't
3212 let them consume issue slots. */
3213 else if (GET_CODE (PATTERN (insn)) != USE
3214 && GET_CODE (PATTERN (insn)) != CLOBBER)
3215 can_issue_more--;
3216 }
3217
3218 /* Advance the DFA to the next cycle. */
3219 advance_one_cycle ();
3220 }
3221 return false;
3222 }
3223
3224 /* Checks if the given node causes resource conflicts when added to PS at
3225 cycle C. If not the node is added to PS and returned; otherwise zero
3226 is returned. Bit N is set in MUST_PRECEDE/MUST_FOLLOW if the node with
3227 cuid N must be come before/after (respectively) the node pointed to by
3228 PS_I when scheduled in the same cycle. */
3229 ps_insn_ptr
3230 ps_add_node_check_conflicts (partial_schedule_ptr ps, int n,
3231 int c, sbitmap must_precede,
3232 sbitmap must_follow)
3233 {
3234 int has_conflicts = 0;
3235 ps_insn_ptr ps_i;
3236
3237 /* First add the node to the PS, if this succeeds check for
3238 conflicts, trying different issue slots in the same row. */
3239 if (! (ps_i = add_node_to_ps (ps, n, c, must_precede, must_follow)))
3240 return NULL; /* Failed to insert the node at the given cycle. */
3241
3242 has_conflicts = ps_has_conflicts (ps, c, c)
3243 || (ps->history > 0
3244 && ps_has_conflicts (ps,
3245 c - ps->history,
3246 c + ps->history));
3247
3248 /* Try different issue slots to find one that the given node can be
3249 scheduled in without conflicts. */
3250 while (has_conflicts)
3251 {
3252 if (! ps_insn_advance_column (ps, ps_i, must_follow))
3253 break;
3254 has_conflicts = ps_has_conflicts (ps, c, c)
3255 || (ps->history > 0
3256 && ps_has_conflicts (ps,
3257 c - ps->history,
3258 c + ps->history));
3259 }
3260
3261 if (has_conflicts)
3262 {
3263 remove_node_from_ps (ps, ps_i);
3264 return NULL;
3265 }
3266
3267 ps->min_cycle = MIN (ps->min_cycle, c);
3268 ps->max_cycle = MAX (ps->max_cycle, c);
3269 return ps_i;
3270 }
3271
3272 /* Calculate the stage count of the partial schedule PS. The calculation
3273 takes into account the rotation amount passed in ROTATION_AMOUNT. */
3274 int
3275 calculate_stage_count (partial_schedule_ptr ps, int rotation_amount)
3276 {
3277 int new_min_cycle = PS_MIN_CYCLE (ps) - rotation_amount;
3278 int new_max_cycle = PS_MAX_CYCLE (ps) - rotation_amount;
3279 int stage_count = CALC_STAGE_COUNT (-1, new_min_cycle, ps->ii);
3280
3281 /* The calculation of stage count is done adding the number of stages
3282 before cycle zero and after cycle zero. */
3283 stage_count += CALC_STAGE_COUNT (new_max_cycle, 0, ps->ii);
3284
3285 return stage_count;
3286 }
3287
3288 /* Rotate the rows of PS such that insns scheduled at time
3289 START_CYCLE will appear in row 0. Updates max/min_cycles. */
3290 void
3291 rotate_partial_schedule (partial_schedule_ptr ps, int start_cycle)
3292 {
3293 int i, row, backward_rotates;
3294 int last_row = ps->ii - 1;
3295
3296 if (start_cycle == 0)
3297 return;
3298
3299 backward_rotates = SMODULO (start_cycle, ps->ii);
3300
3301 /* Revisit later and optimize this into a single loop. */
3302 for (i = 0; i < backward_rotates; i++)
3303 {
3304 ps_insn_ptr first_row = ps->rows[0];
3305 int first_row_length = ps->rows_length[0];
3306
3307 for (row = 0; row < last_row; row++)
3308 {
3309 ps->rows[row] = ps->rows[row + 1];
3310 ps->rows_length[row] = ps->rows_length[row + 1];
3311 }
3312
3313 ps->rows[last_row] = first_row;
3314 ps->rows_length[last_row] = first_row_length;
3315 }
3316
3317 ps->max_cycle -= start_cycle;
3318 ps->min_cycle -= start_cycle;
3319 }
3320
3321 #endif /* INSN_SCHEDULING */
3322 \f
3323 static bool
3324 gate_handle_sms (void)
3325 {
3326 return (optimize > 0 && flag_modulo_sched);
3327 }
3328
3329
3330 /* Run instruction scheduler. */
3331 /* Perform SMS module scheduling. */
3332 static unsigned int
3333 rest_of_handle_sms (void)
3334 {
3335 #ifdef INSN_SCHEDULING
3336 basic_block bb;
3337
3338 /* Collect loop information to be used in SMS. */
3339 cfg_layout_initialize (0);
3340 sms_schedule ();
3341
3342 /* Update the life information, because we add pseudos. */
3343 max_regno = max_reg_num ();
3344
3345 /* Finalize layout changes. */
3346 FOR_EACH_BB (bb)
3347 if (bb->next_bb != EXIT_BLOCK_PTR)
3348 bb->aux = bb->next_bb;
3349 free_dominance_info (CDI_DOMINATORS);
3350 cfg_layout_finalize ();
3351 #endif /* INSN_SCHEDULING */
3352 return 0;
3353 }
3354
3355 struct rtl_opt_pass pass_sms =
3356 {
3357 {
3358 RTL_PASS,
3359 "sms", /* name */
3360 OPTGROUP_NONE, /* optinfo_flags */
3361 gate_handle_sms, /* gate */
3362 rest_of_handle_sms, /* execute */
3363 NULL, /* sub */
3364 NULL, /* next */
3365 0, /* static_pass_number */
3366 TV_SMS, /* tv_id */
3367 0, /* properties_required */
3368 0, /* properties_provided */
3369 0, /* properties_destroyed */
3370 0, /* todo_flags_start */
3371 TODO_df_finish
3372 | TODO_verify_flow
3373 | TODO_verify_rtl_sharing
3374 | TODO_ggc_collect /* todo_flags_finish */
3375 }
3376 };