OSDN Git Service

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