OSDN Git Service

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