OSDN Git Service

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