OSDN Git Service

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