re PR middle-end/37375 ([graphite] Parameter detection and scev only take a surroundi...
[platform/upstream/gcc.git] / gcc / graphite.c
1 /* Gimple Represented as Polyhedra.
2    Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3    Contributed by Sebastian Pop <sebastian.pop@inria.fr>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* This pass converts GIMPLE to GRAPHITE, performs some loop
22    transformations and then converts the resulting representation back
23    to GIMPLE.  
24
25    An early description of this pass can be found in the GCC Summit'06
26    paper "GRAPHITE: Polyhedral Analyses and Optimizations for GCC".
27    The wiki page http://gcc.gnu.org/wiki/Graphite contains pointers to
28    the related work.  
29
30    One important document to read is CLooG's internal manual:
31    http://repo.or.cz/w/cloog-ppl.git?a=blob_plain;f=doc/cloog.texi;hb=HEAD
32    that describes the data structure of loops used in this file, and
33    the functions that are used for transforming the code.  */
34
35 #include "config.h"
36 #include "system.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "ggc.h"
40 #include "tree.h"
41 #include "rtl.h"
42 #include "basic-block.h"
43 #include "diagnostic.h"
44 #include "tree-flow.h"
45 #include "toplev.h"
46 #include "tree-dump.h"
47 #include "timevar.h"
48 #include "cfgloop.h"
49 #include "tree-chrec.h"
50 #include "tree-data-ref.h"
51 #include "tree-scalar-evolution.h"
52 #include "tree-pass.h"
53 #include "domwalk.h"
54 #include "pointer-set.h"
55 #include "gimple.h"
56
57 #ifdef HAVE_cloog
58 #include "cloog/cloog.h"
59 #include "graphite.h"
60
61 static VEC (scop_p, heap) *current_scops;
62
63 /* Debug the list of old induction variables for this SCOP.  */
64
65 void
66 debug_oldivs (scop_p scop)
67 {
68   int i;
69   name_tree oldiv;
70
71   fprintf (stderr, "Old IVs:");
72
73   for (i = 0; VEC_iterate (name_tree, SCOP_OLDIVS (scop), i, oldiv); i++)
74     {
75       fprintf (stderr, "(");
76       print_generic_expr (stderr, oldiv->t, 0);
77       fprintf (stderr, ", %s, %d)\n", oldiv->name, oldiv->loop->num);
78     }
79   fprintf (stderr, "\n");
80 }
81
82 /* Debug the loops around basic block GB.  */
83
84 void
85 debug_loop_vec (graphite_bb_p gb)
86 {
87   int i;
88   loop_p loop;
89
90   fprintf (stderr, "Loop Vec:");
91
92   for (i = 0; VEC_iterate (loop_p, GBB_LOOPS (gb), i, loop); i++)
93     fprintf (stderr, "%d: %d, ", i, loop ? loop->num : -1);
94
95   fprintf (stderr, "\n");
96 }
97
98 /* Push (IV, NAME) on STACK.  */
99
100 static void 
101 loop_iv_stack_push (loop_iv_stack stack, tree iv, const char *name)
102 {
103   name_tree named_iv = XNEW (struct name_tree);
104
105   named_iv->t = iv;
106   named_iv->name = name;
107   VEC_safe_push (name_tree, heap, *stack, named_iv);
108 }
109
110 /* Pops an element out of STACK.  */
111
112 static void
113 loop_iv_stack_pop (loop_iv_stack stack)
114 {
115   VEC_pop (name_tree, *stack);
116 }
117
118 /* Get the IV at INDEX in STACK.  */
119
120 static tree
121 loop_iv_stack_get_iv (loop_iv_stack stack, int index)
122 {
123   name_tree named_iv = VEC_index (name_tree, *stack, index);
124
125   return named_iv->t;
126 }
127
128 /* Get the IV from its NAME in STACK.  */
129
130 static tree
131 loop_iv_stack_get_iv_from_name (loop_iv_stack stack, const char* name)
132 {
133   int i;
134   name_tree iv;
135
136   for (i = 0; VEC_iterate (name_tree, *stack, i, iv); i++)
137     if (!strcmp (name, iv->name))
138       return iv->t;
139
140   return NULL;
141 }
142
143 /* Prints on stderr the contents of STACK.  */
144
145 void
146 loop_iv_stack_debug (loop_iv_stack stack)
147 {
148   int i;
149   name_tree iv;
150   bool first = true;
151
152   fprintf (stderr, "(");
153
154   for (i = 0; VEC_iterate (name_tree, *stack, i, iv); i++)
155     {
156       if (first) 
157         first = false;
158       else
159         fprintf (stderr, " ");
160       fprintf (stderr, "%s:", iv->name);
161       print_generic_expr (stderr, iv->t, 0);
162     }
163
164   fprintf (stderr, ")\n");
165 }
166
167 /* In SCOP, get the induction variable from NAME.  OLD is the original
168    loop that contained the definition of NAME.  */
169
170 static name_tree
171 get_old_iv_from_ssa_name (scop_p scop, loop_p old, tree name)
172 {
173   tree var = SSA_NAME_VAR (name);
174   int i;
175   name_tree oldiv;
176   
177   for (i = 0; VEC_iterate (name_tree, SCOP_OLDIVS (scop), i, oldiv); i++)
178     {
179       loop_p current = old;
180
181       while (current)
182         {
183           if (var == oldiv->t
184               && oldiv->loop == current)
185             return oldiv;
186
187           current = loop_outer (current);
188         }
189     }
190   return NULL;
191
192 }
193
194 /* Returns a new loop_to_cloog_loop_str structure.  */
195
196 static inline struct loop_to_cloog_loop_str *
197 new_loop_to_cloog_loop_str (int loop_num,
198                             int loop_position,
199                             CloogLoop *cloog_loop)
200 {
201   struct loop_to_cloog_loop_str *result;
202
203   result = XNEW (struct loop_to_cloog_loop_str);
204   result->loop_num = loop_num;
205   result->cloog_loop = cloog_loop;
206   result->loop_position = loop_position;
207
208   return result;
209 }
210
211 /* Hash function for SCOP_LOOP2CLOOG_LOOP hash table.  */
212
213 static hashval_t
214 hash_loop_to_cloog_loop (const void *elt)
215 {
216   return ((const struct loop_to_cloog_loop_str *) elt)->loop_num;
217 }
218
219 /* Equality function for SCOP_LOOP2CLOOG_LOOP hash table.  */
220
221 static int
222 eq_loop_to_cloog_loop (const void *el1, const void *el2)
223 {
224   const struct loop_to_cloog_loop_str *elt1, *elt2;
225
226   elt1 = (const struct loop_to_cloog_loop_str *) el1;
227   elt2 = (const struct loop_to_cloog_loop_str *) el2;
228   return elt1->loop_num == elt2->loop_num;
229 }
230
231 /* Compares two graphite bbs and returns an integer less than, equal to, or
232    greater than zero if the first argument is considered to be respectively
233    less than, equal to, or greater than the second. 
234    We compare using the lexicographic order of the static schedules.  */
235
236 static int 
237 gbb_compare (const void *p_1, const void *p_2)
238 {
239   const struct graphite_bb *const gbb_1
240     = *(const struct graphite_bb *const*) p_1;
241   const struct graphite_bb *const gbb_2
242     = *(const struct graphite_bb *const*) p_2;
243
244   return lambda_vector_compare (GBB_STATIC_SCHEDULE (gbb_1),
245                                 gbb_nb_loops (gbb_1) + 1,
246                                 GBB_STATIC_SCHEDULE (gbb_2),
247                                 gbb_nb_loops (gbb_2) + 1);
248 }
249
250 /* Sort graphite bbs in SCOP.  */
251
252 static void
253 graphite_sort_gbbs (scop_p scop)
254 {
255   VEC (graphite_bb_p, heap) *bbs = SCOP_BBS (scop);
256
257   qsort (VEC_address (graphite_bb_p, bbs),
258          VEC_length (graphite_bb_p, bbs),
259          sizeof (graphite_bb_p), gbb_compare);
260 }
261
262 /* Dump conditions of a graphite basic block GBB on FILE.  */
263
264 static void
265 dump_gbb_conditions (FILE *file, graphite_bb_p gbb)
266 {
267   int i;
268   gimple stmt;
269   VEC (gimple, heap) *conditions = GBB_CONDITIONS (gbb);
270   
271   if (VEC_empty (gimple, conditions))
272     return;
273
274   fprintf (file, "\tbb %d\t: cond = {", GBB_BB (gbb)->index);
275
276   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
277     print_gimple_stmt (file, stmt, 0, 0);
278
279   fprintf (file, "}\n");
280 }
281
282 /* Converts the graphite scheduling function into a cloog scattering
283    matrix.  This scattering matrix is used to limit the possible cloog
284    output to valid programs in respect to the scheduling function. 
285
286    SCATTERING_DIMENSIONS specifies the dimensionality of the scattering
287    matrix. CLooG 0.14.0 and previous versions require, that all scattering
288    functions of one CloogProgram have the same dimensionality, therefore we
289    allow to specify it. (Should be removed in future versions)  */
290
291 static CloogMatrix *
292 schedule_to_scattering (graphite_bb_p gb, int scattering_dimensions) 
293 {
294   int i;
295   scop_p scop = GBB_SCOP (gb);
296
297   int nb_iterators = gbb_nb_loops (gb);
298
299   /* The cloog scattering matrix consists of these colums:
300      1                        col  = Eq/Inq,
301      scattering_dimensions    cols = Scattering dimensions,
302      nb_iterators             cols = bb's iterators,
303      scop_nb_params        cols = Parameters,
304      1                        col  = Constant 1.
305
306      Example:
307
308      scattering_dimensions = 5
309      max_nb_iterators = 2
310      nb_iterators = 1 
311      scop_nb_params = 2
312
313      Schedule:
314      ? i
315      4 5
316
317      Scattering Matrix:
318      s1  s2  s3  s4  s5  i   p1  p2  1 
319      1   0   0   0   0   0   0   0  -4  = 0
320      0   1   0   0   0  -1   0   0   0  = 0
321      0   0   1   0   0   0   0   0  -5  = 0  */
322   int nb_params = scop_nb_params (scop);
323   int nb_cols = 1 + scattering_dimensions + nb_iterators + nb_params + 1;
324   int col_const = nb_cols - 1; 
325   int col_iter_offset = 1 + scattering_dimensions;
326
327   CloogMatrix *scat = cloog_matrix_alloc (scattering_dimensions, nb_cols);
328
329   gcc_assert (scattering_dimensions >= nb_iterators * 2 + 1);
330
331   /* Initialize the identity matrix.  */
332   for (i = 0; i < scattering_dimensions; i++)
333     value_set_si (scat->p[i][i + 1], 1);
334
335   /* Textual order outside the first loop */
336   value_set_si (scat->p[0][col_const], -GBB_STATIC_SCHEDULE (gb)[0]);
337
338   /* For all surrounding loops.  */
339   for (i = 0;  i < nb_iterators; i++)
340     {
341       int schedule = GBB_STATIC_SCHEDULE (gb)[i + 1];
342
343       /* Iterations of this loop.  */
344       value_set_si (scat->p[2 * i + 1][col_iter_offset + i], -1);
345
346       /* Textual order inside this loop.  */
347       value_set_si (scat->p[2 * i + 2][col_const], -schedule);
348     }
349
350   return scat; 
351 }
352
353 /* Print the schedules of GB to FILE with INDENT white spaces before.
354    VERBOSITY determines how verbose the code pretty printers are.  */
355
356 void
357 print_graphite_bb (FILE *file, graphite_bb_p gb, int indent, int verbosity)
358 {
359   CloogMatrix *scattering;
360   int i;
361   loop_p loop;
362   fprintf (file, "\nGBB (\n");
363
364   print_loops_bb (file, GBB_BB (gb), indent+2, verbosity);
365
366   if (GBB_DOMAIN (gb))
367     {
368       fprintf (file, "       (domain: \n");
369       cloog_matrix_print (dump_file, GBB_DOMAIN (gb));
370       fprintf (file, "       )\n");
371     }
372
373   if (GBB_STATIC_SCHEDULE (gb))
374     {
375       fprintf (file, "       (static schedule: ");
376       print_lambda_vector (file, GBB_STATIC_SCHEDULE (gb),
377                            gbb_nb_loops (gb) + 1);
378       fprintf (file, "       )\n");
379     }
380
381   if (GBB_LOOPS (gb))
382     {
383       fprintf (file, "       (contained loops: \n");
384       for (i = 0; VEC_iterate (loop_p, GBB_LOOPS (gb), i, loop); i++)
385         if (loop == NULL)
386           fprintf (file, "       iterator %d   =>  NULL \n", i); 
387         else
388           fprintf (file, "       iterator %d   =>  loop %d \n", i,
389                    loop->num);
390       fprintf (file, "       )\n");
391     }
392
393   if (GBB_DATA_REFS (gb))
394     dump_data_references (file, GBB_DATA_REFS (gb));
395
396   if (GBB_CONDITIONS (gb))
397     {
398       fprintf (file, "       (conditions: \n");
399       dump_gbb_conditions (dump_file, gb);
400       fprintf (file, "       )\n");
401     }
402
403   if (GBB_SCOP (gb)
404       && GBB_STATIC_SCHEDULE (gb))
405     {
406       fprintf (file, "       (scattering: \n");
407       scattering = schedule_to_scattering (gb, 2 * gbb_nb_loops (gb) + 1);
408       cloog_matrix_print (file, scattering);
409       cloog_matrix_free (scattering);
410       fprintf (file, "       )\n");
411     }
412
413   fprintf (file, ")\n");
414 }
415
416 /* Print to STDERR the schedules of GB with VERBOSITY level.  */
417
418 void
419 debug_gbb (graphite_bb_p gb, int verbosity)
420 {
421   print_graphite_bb (stderr, gb, 0, verbosity);
422 }
423
424
425 /* Print SCOP to FILE.  VERBOSITY determines how verbose the pretty
426    printers are.  */
427
428 static void
429 print_scop (FILE *file, scop_p scop, int verbosity)
430 {
431   if (scop == NULL)
432     return;
433
434   fprintf (file, "\nSCoP_%d_%d (\n",
435            SCOP_ENTRY (scop)->index, SCOP_EXIT (scop)->index);
436
437   fprintf (file, "       (cloog: \n");
438   cloog_program_print (file, SCOP_PROG (scop));
439   fprintf (file, "       )\n");
440
441   if (SCOP_BBS (scop))
442     {
443       graphite_bb_p gb;
444       int i;
445
446       for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
447         print_graphite_bb (file, gb, 0, verbosity);
448     }
449
450   fprintf (file, ")\n");
451 }
452
453 /* Print all the SCOPs to FILE.  VERBOSITY determines how verbose the
454    code pretty printers are.  */
455
456 static void
457 print_scops (FILE *file, int verbosity)
458 {
459   int i;
460   scop_p scop;
461
462   for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
463     print_scop (file, scop, verbosity);
464 }
465
466 /* Debug SCOP.  VERBOSITY determines how verbose the code pretty
467    printers are. */
468
469 void
470 debug_scop (scop_p scop, int verbosity)
471 {
472   print_scop (stderr, scop, verbosity);
473 }
474
475 /* Debug all SCOPs from CURRENT_SCOPS.  VERBOSITY determines how
476    verbose the code pretty printers are.  */
477
478 void 
479 debug_scops (int verbosity)
480 {
481   print_scops (stderr, verbosity);
482 }
483
484 /* Return true when BB is contained in SCOP.  */
485
486 static inline bool
487 bb_in_scop_p (basic_block bb, scop_p scop)
488 {
489   return bitmap_bit_p (SCOP_BBS_B (scop), bb->index);
490 }
491
492 /* Pretty print to FILE the SCOP in DOT format.  */
493
494 static void 
495 dot_scop_1 (FILE *file, scop_p scop)
496 {
497   edge e;
498   edge_iterator ei;
499   basic_block bb;
500   basic_block entry = SCOP_ENTRY (scop);
501   basic_block exit = SCOP_EXIT (scop);
502     
503   fprintf (file, "digraph SCoP_%d_%d {\n", entry->index,
504            exit->index);
505
506   FOR_ALL_BB (bb)
507     {
508       if (bb == entry)
509         fprintf (file, "%d [shape=triangle];\n", bb->index);
510
511       if (bb == exit)
512         fprintf (file, "%d [shape=box];\n", bb->index);
513
514       if (bb_in_scop_p (bb, scop)) 
515         fprintf (file, "%d [color=red];\n", bb->index);
516
517       FOR_EACH_EDGE (e, ei, bb->succs)
518         fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
519     }
520
521   fputs ("}\n\n", file);
522 }
523
524 /* Display SCOP using dotty.  */
525
526 void
527 dot_scop (scop_p scop)
528 {
529   dot_scop_1 (stderr, scop);
530 }
531
532 /* Pretty print all SCoPs in DOT format and mark them with different colors.
533    If there are not enough colors, paint later SCoPs gray.
534    Special nodes:
535    - "*" after the node number: entry of a SCoP,
536    - "#" after the node number: exit of a SCoP,
537    - "()" entry or exit not part of SCoP.  */
538
539 static void
540 dot_all_scops_1 (FILE *file)
541 {
542   basic_block bb;
543   edge e;
544   edge_iterator ei;
545   scop_p scop;
546   const char* color;
547   int i;
548
549   /* Disable debugging while printing graph.  */
550   int tmp_dump_flags = dump_flags;
551   dump_flags = 0;
552
553   fprintf (file, "digraph all {\n");
554
555   FOR_ALL_BB (bb)
556     {
557       int part_of_scop = false;
558
559       /* Use HTML for every bb label.  So we are able to print bbs
560          which are part of two different SCoPs, with two different
561          background colors.  */
562       fprintf (file, "%d [label=<\n  <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
563                      bb->index);
564       fprintf (file, "CELLSPACING=\"0\">\n");
565
566       /* Select color for SCoP.  */
567       for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
568         if (bb_in_scop_p (bb, scop)
569             || (SESE_EXIT (SCOP_REGION (scop)) && SCOP_EXIT (scop) == bb)
570             || (SESE_ENTRY (SCOP_REGION (scop)) && SCOP_ENTRY (scop) == bb))
571           {
572             switch (i % 17)
573               {
574               case 0: /* red */
575                 color = "#e41a1c";
576                 break;
577               case 1: /* blue */
578                 color = "#377eb8";
579                 break;
580               case 2: /* green */
581                 color = "#4daf4a";
582                 break;
583               case 3: /* purple */
584                 color = "#984ea3";
585                 break;
586               case 4: /* orange */
587                 color = "#ff7f00";
588                 break;
589               case 5: /* yellow */
590                 color = "#ffff33";
591                 break;
592               case 6: /* brown */
593                 color = "#a65628";
594                 break;
595               case 7: /* rose */
596                 color = "#f781bf";
597                 break;
598               case 8:
599                 color = "#8dd3c7";
600                 break;
601               case 9:
602                 color = "#ffffb3";
603                 break;
604               case 10:
605                 color = "#bebada";
606                 break;
607               case 11:
608                 color = "#fb8072";
609                 break;
610               case 12:
611                 color = "#80b1d3";
612                 break;
613               case 13:
614                 color = "#fdb462";
615                 break;
616               case 14:
617                 color = "#b3de69";
618                 break;
619               case 15:
620                 color = "#fccde5";
621                 break;
622               case 16:
623                 color = "#bc80bd";
624                 break;
625               default: /* gray */
626                 color = "#999999";
627               }
628
629             fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
630         
631             if (!bb_in_scop_p (bb, scop))
632               fprintf (file, " ("); 
633
634             if (SESE_ENTRY (SCOP_REGION (scop))
635                 && SESE_EXIT (SCOP_REGION (scop))
636                 && bb == SCOP_ENTRY (scop)
637                 && bb == SCOP_EXIT (scop))
638               fprintf (file, " %d*# ", bb->index);
639             else if (SESE_ENTRY (SCOP_REGION (scop))
640                      && bb == SCOP_ENTRY (scop))
641               fprintf (file, " %d* ", bb->index);
642             else if (SESE_EXIT (SCOP_REGION (scop))
643                      && bb == SCOP_EXIT (scop))
644               fprintf (file, " %d# ", bb->index);
645             else
646               fprintf (file, " %d ", bb->index);
647
648             if (!bb_in_scop_p (bb, scop))
649               fprintf (file, ")");
650
651             fprintf (file, "</TD></TR>\n");
652
653             part_of_scop  = true;
654           }
655
656       if (!part_of_scop)
657         {
658           fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
659           fprintf (file, " %d </TD></TR>\n", bb->index);
660         }
661
662       fprintf (file, "  </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
663     }
664
665   FOR_ALL_BB (bb)
666     {
667       FOR_EACH_EDGE (e, ei, bb->succs)
668               fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
669     }
670
671   fputs ("}\n\n", file);
672
673   /* Enable debugging again.  */
674   dump_flags = tmp_dump_flags;
675 }
676
677 /* Display all SCoPs using dotty.  */
678
679 void
680 dot_all_scops (void)
681 {
682   /* When debugging, enable the following code.  This cannot be used
683      in production compilers because it calls "system".  */
684 #if 0
685   FILE *stream = fopen ("/tmp/allscops.dot", "w");
686   gcc_assert (stream);
687
688   dot_all_scops_1 (stream);
689   fclose (stream);
690
691   system ("dotty /tmp/allscops.dot");
692 #else
693   dot_all_scops_1 (stderr);
694 #endif
695 }
696
697 /* Returns true when LOOP is in SCOP.  */
698
699 static inline bool 
700 loop_in_scop_p (struct loop *loop, scop_p scop)
701 {
702   return (bb_in_scop_p (loop->header, scop)
703           && bb_in_scop_p (loop->latch, scop));
704 }
705
706 /* Returns the outermost loop in SCOP that contains BB.  */
707
708 static struct loop *
709 outermost_loop_in_scop (scop_p scop, basic_block bb)
710 {
711   struct loop *nest;
712
713   nest = bb->loop_father;
714   while (loop_outer (nest) && loop_in_scop_p (loop_outer (nest), scop))
715     nest = loop_outer (nest);
716
717   return nest;
718 }
719
720 /* Returns the block preceding the entry of SCOP.  */
721
722 static basic_block
723 block_before_scop (scop_p scop)
724 {
725   return SESE_ENTRY (SCOP_REGION (scop))->src;
726 }
727
728 /* Return true when EXPR is an affine function in LOOP with parameters
729    instantiated relative to SCOP_ENTRY.  */
730
731 static bool
732 loop_affine_expr (basic_block scop_entry, struct loop *loop, tree expr)
733 {
734   int n = scop_entry->loop_father->num;
735   tree scev = analyze_scalar_evolution (loop, expr);
736
737   scev = instantiate_scev (scop_entry, loop, scev);
738
739   return (evolution_function_is_invariant_p (scev, n)
740           || evolution_function_is_affine_multivariate_p (scev, n));
741 }
742
743 /* Return true if the operand OP is simple.  */
744
745 static bool
746 is_simple_operand (loop_p loop, gimple stmt, tree op) 
747 {
748   /* It is not a simple operand when it is a declaration,  */
749   if (DECL_P (op)
750       /* or a structure,  */
751       || AGGREGATE_TYPE_P (TREE_TYPE (op))
752       /* or a memory access that cannot be analyzed by the data
753          reference analysis.  */
754       || ((handled_component_p (op) || INDIRECT_REF_P (op))
755           && !stmt_simple_memref_p (loop, stmt, op)))
756     return false;
757
758   return true;
759 }
760
761 /* Return true only when STMT is simple enough for being handled by
762    Graphite.  This depends on SCOP_ENTRY, as the parametetrs are
763    initialized relatively to this basic block.  */
764
765 static bool
766 stmt_simple_for_scop_p (basic_block scop_entry, gimple stmt)
767 {
768   basic_block bb = gimple_bb (stmt);
769   struct loop *loop = bb->loop_father;
770
771   /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
772      Calls have side-effects, except those to const or pure
773      functions.  */
774   if (gimple_has_volatile_ops (stmt)
775       || (gimple_code (stmt) == GIMPLE_CALL
776           && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
777       || (gimple_code (stmt) == GIMPLE_ASM))
778     return false;
779
780   switch (gimple_code (stmt))
781     {
782     case GIMPLE_RETURN:
783     case GIMPLE_LABEL:
784       return true;
785
786     case GIMPLE_COND:
787       {
788         tree op;
789         ssa_op_iter op_iter;
790         enum tree_code code = gimple_cond_code (stmt);
791
792         /* We can only handle this kind of conditional expressions.  
793            For inequalities like "if (i != 3 * k)" we need unions of
794            polyhedrons.  Expressions like  "if (a)" or "if (a == 15)" need
795            them for the else branch.  */
796         if (!(code == LT_EXPR
797               || code == GT_EXPR
798               || code == LE_EXPR
799               || code == GE_EXPR))
800           return false;
801
802         if (!scop_entry)
803           return false;
804
805         FOR_EACH_SSA_TREE_OPERAND (op, stmt, op_iter, SSA_OP_ALL_USES)
806           if (!loop_affine_expr (scop_entry, loop, op))
807             return false;
808
809         return true;
810       }
811
812     case GIMPLE_ASSIGN:
813       {
814         enum tree_code code = gimple_assign_rhs_code (stmt);
815
816         switch (get_gimple_rhs_class (code))
817           {
818           case GIMPLE_UNARY_RHS:
819           case GIMPLE_SINGLE_RHS:
820             return (is_simple_operand (loop, stmt, gimple_assign_lhs (stmt))
821                     && is_simple_operand (loop, stmt, gimple_assign_rhs1 (stmt)));
822
823           case GIMPLE_BINARY_RHS:
824             return (is_simple_operand (loop, stmt, gimple_assign_lhs (stmt))
825                     && is_simple_operand (loop, stmt, gimple_assign_rhs1 (stmt))
826                     && is_simple_operand (loop, stmt, gimple_assign_rhs2 (stmt)));
827
828           case GIMPLE_INVALID_RHS:
829           default:
830             gcc_unreachable ();
831           }
832       }
833
834     case GIMPLE_CALL:
835       {
836         size_t i;
837         size_t n = gimple_call_num_args (stmt);
838         tree lhs = gimple_call_lhs (stmt);
839
840         for (i = 0; i < n; i++)
841           {
842             tree arg = gimple_call_arg (stmt, i);
843
844             if (!(is_simple_operand (loop, stmt, lhs)
845                   && is_simple_operand (loop, stmt, arg)))
846               return false;
847           }
848
849         return true;
850       }
851
852     default:
853       /* These nodes cut a new scope.  */
854       return false;
855     }
856
857   return false;
858 }
859
860 /* Returns the statement of BB that contains a harmful operation: that
861    can be a function call with side effects, the induction variables
862    are not linear with respect to SCOP_ENTRY, etc.  The current open
863    scop should end before this statement.  */
864
865 static gimple
866 harmful_stmt_in_bb (basic_block scop_entry, basic_block bb)
867 {
868   gimple_stmt_iterator gsi;
869
870   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
871     if (!stmt_simple_for_scop_p (scop_entry, gsi_stmt (gsi)))
872       return gsi_stmt (gsi);
873
874   return NULL;
875 }
876
877 /* Store the GRAPHITE representation of BB.  */
878
879 static void
880 new_graphite_bb (scop_p scop, basic_block bb)
881 {
882   struct graphite_bb *gbb = XNEW (struct graphite_bb);
883
884   bb->aux = gbb;
885   GBB_BB (gbb) = bb;
886   GBB_SCOP (gbb) = scop;
887   GBB_DATA_REFS (gbb) = NULL; 
888   GBB_DOMAIN (gbb) = NULL;
889   GBB_CONDITIONS (gbb) = NULL;
890   GBB_CONDITION_CASES (gbb) = NULL;
891   GBB_LOOPS (gbb) = NULL;
892   VEC_safe_push (graphite_bb_p, heap, SCOP_BBS (scop), gbb);
893   bitmap_set_bit (SCOP_BBS_B (scop), bb->index);
894 }
895
896 /* Frees GBB.  */
897
898 static void
899 free_graphite_bb (struct graphite_bb *gbb)
900 {
901   if (GBB_DOMAIN (gbb))
902     cloog_matrix_free (GBB_DOMAIN (gbb));
903
904   free_data_refs (GBB_DATA_REFS (gbb));
905   VEC_free (gimple, heap, GBB_CONDITIONS (gbb));
906   VEC_free (gimple, heap, GBB_CONDITION_CASES (gbb));
907   VEC_free (loop_p, heap, GBB_LOOPS (gbb));
908
909   GBB_BB (gbb)->aux = 0;
910   XDELETE (gbb);
911 }
912
913 /* Creates a new scop starting with ENTRY.  */
914
915 static scop_p
916 new_scop (edge entry)
917 {
918   scop_p scop = XNEW (struct scop);
919
920   SCOP_REGION (scop) = XNEW (struct sese);
921   SESE_ENTRY (SCOP_REGION (scop)) = entry;
922   SESE_EXIT (SCOP_REGION (scop)) = NULL;
923   SCOP_BBS (scop) = VEC_alloc (graphite_bb_p, heap, 3);
924   SCOP_OLDIVS (scop) = VEC_alloc (name_tree, heap, 3);
925   SCOP_BBS_B (scop) = BITMAP_ALLOC (NULL);
926   SCOP_LOOPS (scop) = BITMAP_ALLOC (NULL);
927   SCOP_LOOP_NEST (scop) = VEC_alloc (loop_p, heap, 3);
928   SCOP_PARAMS (scop) = VEC_alloc (name_tree, heap, 3);
929   SCOP_PROG (scop) = cloog_program_malloc ();
930   cloog_program_set_names (SCOP_PROG (scop), cloog_names_malloc ());
931   SCOP_LOOP2CLOOG_LOOP (scop) = htab_create (10, hash_loop_to_cloog_loop,
932                                              eq_loop_to_cloog_loop,
933                                              free);
934   return scop;
935 }
936
937 /* Deletes SCOP.  */
938
939 static void
940 free_scop (scop_p scop)
941 {
942   int i;
943   name_tree p;
944   struct graphite_bb *gb;
945
946   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
947     free_graphite_bb (gb);
948
949   VEC_free (graphite_bb_p, heap, SCOP_BBS (scop));
950   BITMAP_FREE (SCOP_BBS_B (scop));
951   BITMAP_FREE (SCOP_LOOPS (scop));
952   VEC_free (loop_p, heap, SCOP_LOOP_NEST (scop));
953   VEC_free (name_tree, heap, SCOP_OLDIVS (scop));
954   
955   for (i = 0; VEC_iterate (name_tree, SCOP_PARAMS (scop), i, p); i++)
956     free (p);
957
958   VEC_free (name_tree, heap, SCOP_PARAMS (scop));
959   cloog_program_free (SCOP_PROG (scop));
960   htab_delete (SCOP_LOOP2CLOOG_LOOP (scop)); 
961   XDELETE (SCOP_REGION (scop));
962   XDELETE (scop);
963 }
964
965 /* Deletes all scops in SCOPS.  */
966
967 static void
968 free_scops (VEC (scop_p, heap) *scops)
969 {
970   int i;
971   scop_p scop;
972
973   for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
974     free_scop (scop);
975
976   VEC_free (scop_p, heap, scops);
977 }
978
979 typedef enum gbb_type {
980   GBB_UNKNOWN,
981   GBB_LOOP_SING_EXIT_HEADER,
982   GBB_LOOP_MULT_EXIT_HEADER,
983   GBB_LOOP_EXIT,
984   GBB_COND_HEADER,
985   GBB_SIMPLE,
986   GBB_LAST
987 } gbb_type;
988
989 /* Detect the type of BB.  Loop headers are only marked, if they are
990    new.  This means their loop_father is different to LAST_LOOP.
991    Otherwise they are treated like any other bb and their type can be
992    any other type.  */
993
994 static gbb_type
995 get_bb_type (basic_block bb, struct loop *last_loop)
996 {
997   VEC (basic_block, heap) *dom;
998   int nb_dom, nb_suc;
999   struct loop *loop = bb->loop_father;
1000
1001   /* Check, if we entry into a new loop. */
1002   if (loop != last_loop)
1003     {
1004       if (single_exit (loop) != NULL)
1005         return GBB_LOOP_SING_EXIT_HEADER;
1006       else if (loop->num != 0)
1007         return GBB_LOOP_MULT_EXIT_HEADER;
1008       else
1009         return GBB_COND_HEADER;
1010     }
1011
1012   dom = get_dominated_by (CDI_DOMINATORS, bb);
1013   nb_dom = VEC_length (basic_block, dom);
1014   VEC_free (basic_block, heap, dom);
1015   if (nb_dom == 0)
1016     return GBB_LAST;
1017
1018   nb_suc = VEC_length (edge, bb->succs);
1019   if (nb_dom == 1 && nb_suc == 1)
1020     return GBB_SIMPLE;
1021
1022   return GBB_COND_HEADER;
1023 }
1024
1025 /* Moves the scops from SOURCE to TARGET and clean up SOURCE.  */
1026
1027 static void
1028 move_scops (VEC (scop_p, heap) **source, VEC (scop_p, heap) **target)
1029 {
1030   scop_p s;
1031   int i;
1032
1033   for (i = 0; VEC_iterate (scop_p, *source, i, s); i++)
1034     VEC_safe_push (scop_p, heap, *target, s);
1035   
1036   VEC_free (scop_p, heap, *source);
1037 }
1038
1039 /* Store information needed by scopdet_* functions.  */
1040
1041 struct scopdet_info
1042 {
1043   /* Where the last open scop would stop if the current BB is harmful.  */
1044   edge last;
1045
1046   /* Where the next scop would start if the current BB is harmful.  */
1047   edge next;
1048
1049   /* The bb or one of its children contains open loop exits.  That means
1050      loop exit nodes that are not surrounded by a loop dominated by bb.  */ 
1051   bool exits;
1052
1053   /* The bb or one of its children contains only structures we can handle.  */ 
1054   bool difficult;
1055 };
1056
1057 static struct scopdet_info build_scops_1 (edge, VEC (scop_p, heap) **,
1058                                           loop_p);
1059
1060 /* Checks, if a bb can be added to a SCoP.  */
1061
1062 static struct scopdet_info 
1063 scopdet_edge_info (edge ee,
1064                    VEC (scop_p, heap) **scops, gbb_type type, gimple *stmt)
1065                
1066 {
1067   basic_block bb = ee->dest;
1068   struct loop *loop = bb->loop_father;
1069   struct scopdet_info result;
1070   basic_block scop_entry;
1071
1072   if (VEC_length (scop_p, *scops) != 0)
1073     scop_entry = block_before_scop (VEC_last (scop_p, *scops));
1074   else if (loop->header)
1075     scop_entry = loop->header;
1076   else
1077     scop_entry = ENTRY_BLOCK_PTR;
1078
1079   *stmt = harmful_stmt_in_bb (scop_entry, bb);
1080   result.difficult = (*stmt != NULL);
1081   result.last = NULL;
1082
1083   switch (type)
1084     {
1085     case GBB_LAST:
1086       result.next = NULL;
1087       result.exits = false;
1088       result.last = ee;
1089       break;
1090
1091     case GBB_SIMPLE:
1092       result.next = single_succ_edge (bb);
1093       result.exits = false;
1094       result.last = ee;
1095       break;
1096
1097     case GBB_LOOP_SING_EXIT_HEADER:
1098       {
1099         VEC (scop_p, heap) *tmp_scops = VEC_alloc (scop_p, heap, 3);
1100         struct scopdet_info sinfo;
1101
1102         sinfo = build_scops_1 (ee, &tmp_scops, loop);
1103
1104         result.last = single_exit (bb->loop_father);
1105
1106         if (single_succ_p (result.last->dest)
1107             && get_bb_type (result.last->dest, loop) == GBB_SIMPLE)
1108           result.next = single_succ_edge (result.last->dest);
1109         else
1110           result.next = split_block (result.last->dest, NULL);
1111
1112         /* If we do not dominate result.next, remove it.  It's either
1113            the EXIT_BLOCK_PTR, or another bb dominates it and will
1114            call the scop detection for this bb.  */
1115         if (!dominated_by_p (CDI_DOMINATORS, result.next->dest, bb))
1116           result.next = NULL;
1117
1118         if (TREE_CODE (number_of_latch_executions (loop))
1119             == SCEV_NOT_KNOWN)
1120           result.difficult = true;
1121
1122         if (sinfo.difficult)
1123           move_scops (&tmp_scops, scops);
1124         else 
1125           free_scops (tmp_scops);
1126
1127         result.exits = false;
1128         result.difficult |= sinfo.difficult;
1129         break;
1130       }
1131
1132     case GBB_LOOP_MULT_EXIT_HEADER:
1133       {
1134         /* XXX: Handle loop nests with the same header.  */
1135         /* XXX: For now we just do not join loops with multiple exits. If the 
1136            exits lead to the same bb it may be possible to join the loop.  */
1137         VEC (scop_p, heap) *tmp_scops = VEC_alloc (scop_p, heap, 3);
1138         VEC (edge, heap) *exits = get_loop_exit_edges (loop);
1139         edge e;
1140         int i;
1141         build_scops_1 (ee, &tmp_scops, loop);
1142
1143         for (i = 0; VEC_iterate (edge, exits, i, e); i++)
1144           if (dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
1145               && e->dest->loop_father == loop_outer (loop))
1146             build_scops_1 (e, &tmp_scops, e->dest->loop_father);
1147
1148         result.next = NULL; 
1149         result.last = NULL;
1150         result.difficult = true;
1151         result.exits = false;
1152         move_scops (&tmp_scops, scops);
1153         VEC_free (edge, heap, exits);
1154         break;
1155       }
1156     case GBB_COND_HEADER:
1157       {
1158         VEC (scop_p, heap) *tmp_scops = VEC_alloc (scop_p, heap, 3);
1159         struct scopdet_info sinfo;
1160         VEC (basic_block, heap) *dominated;
1161         int i;
1162         basic_block dom_bb;
1163         basic_block last_bb = NULL;
1164         edge last_e = NULL;
1165         edge e;
1166         result.exits = false;
1167  
1168         /* First check the successors of BB, and check if it is possible to join
1169            the different branches.  */
1170         for (i = 0; VEC_iterate (edge, bb->succs, i, e); i++)
1171           {
1172             /* Ignore loop exits.  They will be handled after the loop body.  */
1173             if (is_loop_exit (loop, e->dest))
1174               {
1175                 result.exits = true;
1176                 continue;
1177               }
1178
1179             /* Do not follow edges that lead to the end of the
1180                conditions block.  For example, in
1181
1182                |   0
1183                |  /|\
1184                | 1 2 |
1185                | | | |
1186                | 3 4 |
1187                |  \|/
1188                |   6
1189
1190                the edge from 0 => 6.  Only check if all paths lead to
1191                the same node 6.  */
1192
1193             if (!single_pred_p (e->dest))
1194               {
1195                 /* Check, if edge leads directly to the end of this
1196                    condition.  */
1197                 if (!last_bb)
1198                   {
1199                     last_bb = e->dest;
1200                     last_e = e;
1201                   }
1202
1203                 if (e->dest != last_bb)
1204                   result.difficult = true;
1205
1206                 continue;
1207               }
1208
1209             if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
1210               {
1211                 result.difficult = true;
1212                 continue;
1213               }
1214
1215             sinfo = build_scops_1 (e, &tmp_scops, loop);
1216
1217             result.exits |= sinfo.exits;
1218             result.last = sinfo.last;
1219             result.difficult |= sinfo.difficult; 
1220
1221             /* Checks, if all branches end at the same point. 
1222                If that is true, the condition stays joinable.
1223                Have a look at the example above.  */
1224             if (sinfo.last && single_succ_p (sinfo.last->dest))
1225               {
1226                 basic_block next_tmp = single_succ (sinfo.last->dest);
1227                   
1228                 if (!last_bb)
1229                   {
1230                     last_bb = next_tmp;
1231                     last_e = single_succ_edge (sinfo.last->dest);
1232                   }
1233
1234                 if (next_tmp != last_bb)
1235                   result.difficult = true;
1236               }
1237             else
1238               result.difficult = true;
1239           }
1240
1241         /* If the condition is joinable.  */
1242         if (!result.exits && !result.difficult)
1243           {
1244             /* Only return a next pointer if we dominate this pointer.
1245                Otherwise it will be handled by the bb dominating it.  */ 
1246             if (dominated_by_p (CDI_DOMINATORS, last_bb, bb) && last_bb != bb)
1247               result.next = last_e;
1248             else
1249               result.next = NULL; 
1250
1251             move_scops (&tmp_scops, scops);
1252             break;
1253           }
1254
1255         /* Scan remaining bbs dominated by BB.  */
1256         dominated = get_dominated_by (CDI_DOMINATORS, bb);
1257
1258         for (i = 0; VEC_iterate (basic_block, dominated, i, dom_bb); i++)
1259           {
1260             /* Ignore loop exits: they will be handled after the loop body.  */
1261             if (is_loop_exit (loop, dom_bb))
1262               {
1263                 result.exits = true;
1264                 continue;
1265               }
1266
1267             /* Ignore the bbs processed above.  */
1268             if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
1269               continue;
1270
1271             if (single_pred_p (dom_bb))
1272               e = single_pred_edge (dom_bb);
1273             else
1274               e = split_block (dom_bb, NULL);
1275
1276             if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
1277               sinfo = build_scops_1 (e, &tmp_scops, loop_outer (loop));
1278             else
1279               sinfo = build_scops_1 (e, &tmp_scops, loop);
1280                                            
1281                                      
1282             result.exits |= sinfo.exits; 
1283             result.difficult = true;
1284             result.last = NULL;
1285           }
1286
1287         VEC_free (basic_block, heap, dominated);
1288
1289         result.next = NULL; 
1290         move_scops (&tmp_scops, scops);
1291
1292         break;
1293       }
1294
1295     default:
1296       gcc_unreachable ();
1297     }
1298
1299   return result;
1300 }
1301
1302 /* Split EXIT before STMT when STMT is non NULL.  */
1303
1304 static edge
1305 split_difficult_bb (basic_block exit, edge *last, gimple stmt)
1306 {
1307   if (stmt && VEC_length (edge, exit->preds) == 1)
1308     {
1309       edge e;
1310
1311       if (stmt == gsi_stmt (gsi_after_labels (exit)))
1312         stmt = NULL;
1313       else
1314         {
1315           gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1316           gsi_prev (&gsi);
1317           stmt = gsi_stmt (gsi);
1318         }
1319
1320       e = split_block (exit, stmt);
1321       set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
1322       set_immediate_dominator (CDI_POST_DOMINATORS, e->src, e->dest);
1323       exit = e->dest;
1324
1325       if (last)
1326         *last = e;
1327
1328       return e;
1329     }
1330
1331   return NULL;
1332 }
1333
1334 /* End SCOP with edge EXIT.  */
1335
1336 static void
1337 end_scop (scop_p scop, edge exit, bool split_entry)
1338 {
1339   if (split_entry 
1340       && !single_pred_p (SCOP_ENTRY (scop))
1341       && exit->dest->loop_father == SCOP_ENTRY (scop)->loop_father)
1342     SESE_ENTRY (SCOP_REGION (scop)) = split_block (SCOP_ENTRY (scop), NULL);
1343
1344   SESE_EXIT (SCOP_REGION (scop)) = exit;
1345 }
1346
1347 /* Creates the SCoPs and writes entry and exit points for every SCoP.  */
1348
1349 static struct scopdet_info 
1350 build_scops_1 (edge start, VEC (scop_p, heap) **scops, loop_p loop)
1351 {
1352   edge current = start;
1353
1354   bool in_scop = false;
1355   scop_p open_scop = NULL;
1356   gimple stmt;
1357   struct scopdet_info sinfo;
1358
1359   /* Initialize result.  */ 
1360   struct scopdet_info result;
1361   result.exits = false;
1362   result.difficult = false;
1363   result.next = NULL;
1364   result.last = NULL;
1365
1366   /* Loop over the dominance tree.  If we meet a difficult bb, close
1367      the current SCoP.  Loop and condition header start a new layer,
1368      and can only be added if all bbs in deeper layers are simple.  */
1369   while (current != NULL)
1370     {
1371       sinfo = scopdet_edge_info (current, scops,
1372                                  get_bb_type (current->dest, loop), &stmt);
1373
1374       if (!in_scop && !(sinfo.exits || sinfo.difficult))
1375         {
1376           open_scop = new_scop (current);
1377
1378           VEC_safe_push (scop_p, heap, *scops, open_scop); 
1379           in_scop = true;
1380         }
1381       else if (in_scop && (sinfo.exits || sinfo.difficult))
1382         {
1383           edge exit = split_difficult_bb (current->dest, &sinfo.last, stmt);
1384
1385           if (!exit)
1386             exit = current;
1387
1388           end_scop (open_scop, exit, sinfo.difficult);
1389           in_scop = false;
1390         }
1391
1392       result.difficult |= sinfo.difficult;
1393       result.exits |= sinfo.exits;
1394
1395       current = sinfo.next;
1396     }
1397
1398   /* Finish the SCOP, if it is left open.  The exit is the bb, that
1399      postdominates sinfo.last.  If no such bb exists, we use info.last
1400      or delete the scop.  */
1401   if (in_scop)
1402     {
1403       int i;
1404       edge e;
1405
1406       for (i = 0; VEC_iterate (edge, sinfo.last->dest->succs, i, e); i++)
1407         if (dominated_by_p (CDI_POST_DOMINATORS, sinfo.last->dest, e->dest))
1408           {
1409             edge exit = split_difficult_bb (e->dest, &sinfo.last, stmt);
1410
1411             if (exit)
1412               end_scop (open_scop, exit, sinfo.difficult);
1413             else
1414               end_scop (open_scop, e, sinfo.difficult);
1415
1416             goto done;
1417           }
1418
1419       if (SCOP_ENTRY (open_scop) != sinfo.last->dest)
1420         {
1421           edge exit = split_difficult_bb (sinfo.last->dest, NULL, stmt);
1422
1423           if (exit)
1424             end_scop (open_scop, exit, sinfo.difficult);
1425           else
1426             end_scop (open_scop, sinfo.last, sinfo.difficult);
1427         }
1428       else
1429         {
1430           VEC_pop (scop_p, *scops);
1431           free_scop (open_scop);
1432         }
1433     }
1434
1435  done:
1436   result.last = sinfo.last;
1437
1438   return result;
1439 }
1440
1441 /* Find static control parts.  */
1442
1443 static void
1444 build_scops (void)
1445 {
1446   struct loop *loop = current_loops->tree_root;
1447   build_scops_1 (single_succ_edge (ENTRY_BLOCK_PTR), &current_scops, loop);
1448 }
1449
1450 /* Gather the basic blocks belonging to the SCOP.  */
1451
1452 static void
1453 build_scop_bbs (scop_p scop)
1454 {
1455   basic_block *stack = XNEWVEC (basic_block, n_basic_blocks + 1);
1456   sbitmap visited = sbitmap_alloc (last_basic_block);
1457   int sp = 0;
1458
1459   sbitmap_zero (visited);
1460   stack[sp++] = SCOP_ENTRY (scop);
1461
1462   while (sp)
1463     {
1464       basic_block bb = stack[--sp];
1465       int depth = loop_depth (bb->loop_father);
1466       int num = bb->loop_father->num;
1467       edge_iterator ei;
1468       edge e;
1469
1470       /* Scop's exit is not in the scop.  Exclude also bbs, which are
1471          dominated by the SCoP exit.  These are e.g. loop latches.  */
1472       if (TEST_BIT (visited, bb->index)
1473           || dominated_by_p (CDI_DOMINATORS, bb, SCOP_EXIT (scop))
1474           /* Every block in the scop is dominated by scop's entry.  */
1475           || !dominated_by_p (CDI_DOMINATORS, bb, SCOP_ENTRY (scop)))
1476         continue;
1477
1478       new_graphite_bb (scop, bb);
1479       SET_BIT (visited, bb->index);
1480
1481       /* First push the blocks that have to be processed last.  Note
1482          that this means that the order in which the code is organized
1483          below is important: do not reorder the following code.  */
1484       FOR_EACH_EDGE (e, ei, bb->succs)
1485         if (! TEST_BIT (visited, e->dest->index)
1486             && (int) loop_depth (e->dest->loop_father) < depth)
1487           stack[sp++] = e->dest;
1488
1489       FOR_EACH_EDGE (e, ei, bb->succs)
1490         if (! TEST_BIT (visited, e->dest->index)
1491             && (int) loop_depth (e->dest->loop_father) == depth
1492             && e->dest->loop_father->num != num)
1493           stack[sp++] = e->dest;
1494
1495       FOR_EACH_EDGE (e, ei, bb->succs)
1496         if (! TEST_BIT (visited, e->dest->index)
1497             && (int) loop_depth (e->dest->loop_father) == depth
1498             && e->dest->loop_father->num == num
1499             && EDGE_COUNT (e->dest->preds) > 1)
1500           stack[sp++] = e->dest;
1501
1502       FOR_EACH_EDGE (e, ei, bb->succs)
1503         if (! TEST_BIT (visited, e->dest->index)
1504             && (int) loop_depth (e->dest->loop_father) == depth
1505             && e->dest->loop_father->num == num
1506             && EDGE_COUNT (e->dest->preds) == 1)
1507           stack[sp++] = e->dest;
1508
1509       FOR_EACH_EDGE (e, ei, bb->succs)
1510         if (! TEST_BIT (visited, e->dest->index)
1511             && (int) loop_depth (e->dest->loop_father) > depth)
1512           stack[sp++] = e->dest;
1513     }
1514
1515   free (stack);
1516   sbitmap_free (visited);
1517 }
1518
1519
1520 /* Record LOOP as occuring in SCOP.  */
1521
1522 static void
1523 scop_record_loop (scop_p scop, struct loop *loop)
1524 {
1525   loop_p parent;
1526   tree induction_var;
1527
1528   if (bitmap_bit_p (SCOP_LOOPS (scop), loop->num))
1529     return;
1530
1531   parent = loop_outer (loop);
1532   induction_var = find_induction_var_from_exit_cond (loop);
1533
1534   if (!bb_in_scop_p (parent->latch, scop))
1535     parent = NULL;
1536
1537   if (induction_var != NULL_TREE)
1538     {
1539       name_tree oldiv = XNEW (struct name_tree);
1540       oldiv->t = SSA_NAME_VAR (induction_var);
1541       if (DECL_NAME (oldiv->t))
1542         oldiv->name = IDENTIFIER_POINTER (DECL_NAME (oldiv->t));
1543       else
1544         {
1545           char *n = XNEWVEC (char, 16);
1546           sprintf (n, "D.%u", DECL_UID (oldiv->t));
1547           oldiv->name = n;
1548         }
1549       oldiv->loop = loop;
1550
1551       VEC_safe_push (name_tree, heap, SCOP_OLDIVS (scop), oldiv);
1552     }
1553
1554   bitmap_set_bit (SCOP_LOOPS (scop), loop->num);
1555   VEC_safe_push (loop_p, heap, SCOP_LOOP_NEST (scop), loop);
1556 }
1557
1558 /* Build the loop nests contained in SCOP.  */
1559
1560 static void
1561 build_scop_loop_nests (scop_p scop)
1562 {
1563   unsigned i;
1564   graphite_bb_p gb;
1565   struct loop *loop0, *loop1;
1566
1567   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
1568     {
1569       struct loop *loop = gbb_loop (gb);
1570
1571       /* Only add loops, if they are completely contained in the SCoP.  */
1572       if (loop->header == GBB_BB (gb)
1573           && bb_in_scop_p (loop->latch, scop))
1574         scop_record_loop (scop, gbb_loop (gb));
1575     }
1576
1577   /* Make sure that the loops in the SCOP_LOOP_NEST are ordered.  It
1578      can be the case that an inner loop is inserted before an outer
1579      loop.  To avoid this, semi-sort once.  */
1580   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop0); i++)
1581     {
1582       if (VEC_length (loop_p, SCOP_LOOP_NEST (scop)) == i + 1)
1583         break;
1584
1585       loop1 = VEC_index (loop_p, SCOP_LOOP_NEST (scop), i + 1);
1586       if (loop0->num > loop1->num)
1587         {
1588           VEC_replace (loop_p, SCOP_LOOP_NEST (scop), i, loop1);
1589           VEC_replace (loop_p, SCOP_LOOP_NEST (scop), i + 1, loop0);
1590         }
1591     }
1592 }
1593
1594 /* Calculate the number of loops around GB in the current SCOP.  */
1595
1596 static inline int
1597 nb_loops_around_gb (graphite_bb_p gb)
1598 {
1599   scop_p scop = GBB_SCOP (gb);
1600   struct loop *l = gbb_loop (gb);
1601   int d = 0;
1602
1603   for (; loop_in_scop_p (l, scop); d++, l = loop_outer (l));
1604
1605   return d;
1606 }
1607
1608 /* Build for BB the static schedule.
1609
1610    The STATIC_SCHEDULE is defined like this:
1611
1612    A
1613    for (i: ...)
1614      {
1615        for (j: ...)
1616          {
1617            B
1618            C 
1619          }
1620
1621        for (k: ...)
1622          {
1623            D
1624            E 
1625          }
1626      }
1627    F
1628
1629    Static schedules for A to F:
1630
1631      DEPTH
1632      0 1 2 
1633    A 0
1634    B 1 0 0
1635    C 1 0 1
1636    D 1 1 0
1637    E 1 1 1 
1638    F 2
1639 */
1640
1641 static void
1642 build_scop_canonical_schedules (scop_p scop)
1643 {
1644   int i, j;
1645   graphite_bb_p gb;
1646   int nb = scop_nb_loops (scop) + 1;
1647
1648   SCOP_STATIC_SCHEDULE (scop) = lambda_vector_new (nb);
1649
1650   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
1651     {
1652       int offset = nb_loops_around_gb (gb);
1653
1654       /* After leaving a loop, it is possible that the schedule is not
1655          set at zero.  This loop reinitializes components located
1656          after OFFSET.  */
1657
1658       for (j = offset + 1; j < nb; j++)
1659         if (SCOP_STATIC_SCHEDULE (scop)[j])
1660           {
1661             memset (&(SCOP_STATIC_SCHEDULE (scop)[j]), 0,
1662                     sizeof (int) * (nb - j));
1663             ++SCOP_STATIC_SCHEDULE (scop)[offset];
1664             break;
1665           }
1666
1667       GBB_STATIC_SCHEDULE (gb) = lambda_vector_new (offset + 1);
1668       lambda_vector_copy (SCOP_STATIC_SCHEDULE (scop), 
1669                           GBB_STATIC_SCHEDULE (gb), offset + 1);
1670
1671       ++SCOP_STATIC_SCHEDULE (scop)[offset];
1672     }
1673 }
1674
1675 /* Build the LOOPS vector for all bbs in SCOP.  */
1676
1677 static void
1678 build_bb_loops (scop_p scop)
1679 {
1680   graphite_bb_p gb;
1681   int i;
1682
1683   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
1684     {
1685       loop_p loop;
1686       int depth; 
1687
1688       depth = nb_loops_around_gb (gb) - 1; 
1689
1690       GBB_LOOPS (gb) = VEC_alloc (loop_p, heap, 3);
1691       VEC_safe_grow_cleared (loop_p, heap, GBB_LOOPS (gb), depth + 1);
1692
1693       loop = GBB_BB (gb)->loop_father;  
1694
1695       while (scop_contains_loop (scop, loop))
1696         {
1697           VEC_replace (loop_p, GBB_LOOPS (gb), depth, loop);
1698           loop = loop_outer (loop);
1699           depth--;
1700         }
1701     }
1702 }
1703
1704 /* Get the index for parameter VAR in SCOP.  */
1705
1706 static int
1707 param_index (tree var, scop_p scop)
1708 {
1709   int i;
1710   name_tree p;
1711   name_tree nvar;
1712
1713   gcc_assert (TREE_CODE (var) == SSA_NAME);
1714
1715   for (i = 0; VEC_iterate (name_tree, SCOP_PARAMS (scop), i, p); i++)
1716     if (p->t == var)
1717       return i;
1718
1719   nvar = XNEW (struct name_tree);
1720   nvar->t = var;
1721   nvar->name = NULL;
1722   VEC_safe_push (name_tree, heap, SCOP_PARAMS (scop), nvar);
1723   return VEC_length (name_tree, SCOP_PARAMS (scop)) - 1;
1724 }
1725
1726 /* Scan EXPR and translate it to an inequality vector INEQ that will
1727    be added, or subtracted, in the constraint domain matrix C at row
1728    R.  K is the number of columns for loop iterators in C. */ 
1729
1730 static void
1731 scan_tree_for_params (scop_p s, tree e, CloogMatrix *c, int r, Value k,
1732                       bool subtract)
1733 {
1734   int cst_col, param_col;
1735
1736   if (e == chrec_dont_know)
1737     return;
1738
1739   switch (TREE_CODE (e))
1740     {
1741     case POLYNOMIAL_CHREC:
1742       {
1743         tree left = CHREC_LEFT (e);
1744         tree right = CHREC_RIGHT (e);
1745         int var = CHREC_VARIABLE (e);
1746
1747         if (TREE_CODE (right) != INTEGER_CST)
1748           return;
1749
1750         if (c)
1751           {
1752             int loop_col = scop_gimple_loop_depth (s, get_loop (var)) + 1;
1753
1754             if (subtract)
1755               value_sub_int (c->p[r][loop_col], c->p[r][loop_col],
1756                              int_cst_value (right));
1757             else
1758               value_add_int (c->p[r][loop_col], c->p[r][loop_col],
1759                              int_cst_value (right));
1760           }
1761
1762         switch (TREE_CODE (left))
1763           {
1764           case POLYNOMIAL_CHREC:
1765             scan_tree_for_params (s, left, c, r, k, subtract);
1766             return;
1767
1768           case INTEGER_CST:
1769             /* Constant part.  */
1770             if (c)
1771               {
1772                 int v = int_cst_value (left);
1773                 cst_col = c->NbColumns - 1;
1774
1775                 if (v < 0)
1776                   {
1777                     v = -v;
1778                     subtract = subtract ? false : true;
1779                   }
1780
1781                 if (subtract)
1782                   value_sub_int (c->p[r][cst_col], c->p[r][cst_col], v);
1783                 else
1784                   value_add_int (c->p[r][cst_col], c->p[r][cst_col], v);
1785               }
1786             return;
1787
1788           default:
1789             scan_tree_for_params (s, left, c, r, k, subtract);
1790             return;
1791           }
1792       }
1793       break;
1794
1795     case MULT_EXPR:
1796       if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1797         {
1798           Value val;
1799
1800           gcc_assert (host_integerp (TREE_OPERAND (e, 1), 0));
1801
1802           value_init (val);
1803           value_set_si (val, int_cst_value (TREE_OPERAND (e, 1)));
1804           value_multiply (k, k, val);
1805           value_clear (val);
1806           scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
1807         }
1808       else
1809         {
1810           Value val;
1811
1812           gcc_assert (host_integerp (TREE_OPERAND (e, 0), 0));
1813
1814           value_init (val);
1815           value_set_si (val, int_cst_value (TREE_OPERAND (e, 0)));
1816           value_multiply (k, k, val);
1817           value_clear (val);
1818           scan_tree_for_params (s, TREE_OPERAND (e, 1), c, r, k, subtract);
1819         }
1820       break;
1821
1822     case PLUS_EXPR:
1823       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
1824       scan_tree_for_params (s, TREE_OPERAND (e, 1), c, r, k, subtract);
1825       break;
1826
1827     case MINUS_EXPR:
1828       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
1829       value_oppose (k, k);
1830       scan_tree_for_params (s, TREE_OPERAND (e, 1), c, r, k, subtract);
1831       break;
1832
1833     case NEGATE_EXPR:
1834       value_oppose (k, k);
1835       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
1836       break;
1837
1838     case SSA_NAME:
1839       param_col = param_index (e, s);
1840
1841       if (c)
1842         {
1843           param_col += c->NbColumns - scop_nb_params (s) - 1;
1844
1845           if (subtract)
1846             value_subtract (c->p[r][param_col], c->p[r][param_col], k);
1847           else
1848             value_addto (c->p[r][param_col], c->p[r][param_col], k);
1849         }
1850       break;
1851
1852     case INTEGER_CST:
1853       if (c)
1854         {
1855           int v = int_cst_value (e);
1856           cst_col = c->NbColumns - 1;
1857
1858           if (v < 0)
1859           {
1860             v = -v;
1861             subtract = subtract ? false : true;
1862           }
1863                 
1864           if (subtract)
1865             value_sub_int (c->p[r][cst_col], c->p[r][cst_col], v); 
1866           else
1867             value_add_int (c->p[r][cst_col], c->p[r][cst_col], v);
1868         }
1869       break;
1870
1871     case NOP_EXPR:
1872     case CONVERT_EXPR:
1873     case NON_LVALUE_EXPR:
1874       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
1875       break;
1876
1877     default:
1878       gcc_unreachable ();
1879       break;
1880     }
1881 }
1882
1883 /* Data structure for idx_record_params.  */
1884
1885 struct irp_data
1886 {
1887   struct loop *loop;
1888   scop_p scop;
1889 };
1890
1891 /* For a data reference with an ARRAY_REF as its BASE, record the
1892    parameters occurring in IDX.  DTA is passed in as complementary
1893    information, and is used by the automatic walker function.  This
1894    function is a callback for for_each_index.  */
1895
1896 static bool
1897 idx_record_params (tree base, tree *idx, void *dta)
1898 {
1899   struct irp_data *data = (struct irp_data *) dta;
1900
1901   if (TREE_CODE (base) != ARRAY_REF)
1902     return true;
1903
1904   if (TREE_CODE (*idx) == SSA_NAME)
1905     {
1906       tree scev;
1907       scop_p scop = data->scop;
1908       struct loop *loop = data->loop;
1909       Value one;
1910
1911       scev = analyze_scalar_evolution (loop, *idx);
1912       scev = instantiate_scev (block_before_scop (scop), loop, scev);
1913
1914       value_init (one);
1915       value_set_si (one, 1);
1916       scan_tree_for_params (scop, scev, NULL, 0, one, false);
1917       value_clear (one);
1918     }
1919
1920   return true;
1921 }
1922
1923 /* Find parameters with respect to SCOP in BB. We are looking in memory
1924    access functions, conditions and loop bounds.  */
1925
1926 static void
1927 find_params_in_bb (scop_p scop, basic_block bb)
1928 {
1929   int i;
1930   data_reference_p dr;
1931   VEC (data_reference_p, heap) *drs;
1932   gimple_stmt_iterator gsi;
1933   struct loop *nest = outermost_loop_in_scop (scop, bb);
1934
1935   /* Find the parameters used in the memory access functions.  */
1936   drs = VEC_alloc (data_reference_p, heap, 5);
1937   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1938     find_data_references_in_stmt (nest, gsi_stmt (gsi), &drs);
1939
1940   for (i = 0; VEC_iterate (data_reference_p, drs, i, dr); i++)
1941     {
1942       struct irp_data irp;
1943
1944       irp.loop = bb->loop_father;
1945       irp.scop = scop;
1946       for_each_index (&dr->ref, idx_record_params, &irp);
1947       free_data_ref (dr);
1948     }
1949
1950   VEC_free (data_reference_p, heap, drs);
1951
1952   /* Find parameters in conditional statements.  */ 
1953   gsi = gsi_last_bb (bb);
1954   if (!gsi_end_p (gsi))
1955     {
1956       gimple stmt = gsi_stmt (gsi);
1957
1958       if (gimple_code (stmt) == GIMPLE_COND)
1959         {
1960           Value one;
1961           loop_p loop = bb->loop_father;
1962
1963           tree lhs, rhs;
1964           
1965           lhs = gimple_cond_lhs (stmt);
1966           lhs = analyze_scalar_evolution (loop, lhs);
1967           lhs = instantiate_scev (block_before_scop (scop), loop, lhs);
1968
1969           rhs = gimple_cond_rhs (stmt);
1970           rhs = analyze_scalar_evolution (loop, rhs);
1971           rhs = instantiate_scev (block_before_scop (scop), loop, rhs);
1972
1973           value_init (one);
1974           scan_tree_for_params (scop, lhs, NULL, 0, one, false);
1975           value_set_si (one, 1);
1976           scan_tree_for_params (scop, rhs, NULL, 0, one, false);
1977           value_clear (one);
1978        }
1979     }
1980 }
1981
1982 /* Saves in NV the name of variable P->T.  */
1983
1984 static void
1985 save_var_name (char **nv, int i, name_tree p)
1986 {
1987   const char *name = get_name (SSA_NAME_VAR (p->t));
1988
1989   if (name)
1990     {
1991       nv[i] = XNEWVEC (char, strlen (name) + 12);
1992       sprintf (nv[i], "%s_%12d", name, SSA_NAME_VERSION (p->t));
1993     }
1994   else
1995     {
1996       nv[i] = XNEWVEC (char, 12);
1997       sprintf (nv[i], "T_%12d", SSA_NAME_VERSION (p->t));
1998     }
1999
2000   p->name = nv[i];
2001 }
2002
2003 /* Return the maximal loop depth in SCOP.  */
2004
2005 static int
2006 scop_max_loop_depth (scop_p scop)
2007 {
2008   int i;
2009   graphite_bb_p gbb;
2010   int max_nb_loops = 0;
2011
2012   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++) 
2013     {    
2014       int nb_loops = gbb_nb_loops (gbb);
2015       if (max_nb_loops < nb_loops)
2016         max_nb_loops = nb_loops;
2017     }    
2018
2019   return max_nb_loops;
2020 }
2021
2022 /* Initialize Cloog's parameter names from the names used in GIMPLE.
2023    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
2024    from 0 to scop_nb_loops (scop).  */
2025
2026 static void
2027 initialize_cloog_names (scop_p scop)
2028 {
2029   int i, nb_params = VEC_length (name_tree, SCOP_PARAMS (scop));
2030   char **params = XNEWVEC (char *, nb_params);
2031   int nb_iterators = scop_max_loop_depth (scop);
2032   int nb_scattering= cloog_program_nb_scattdims (SCOP_PROG (scop));
2033   char **iterators = XNEWVEC (char *, nb_iterators * 2);
2034   char **scattering = XNEWVEC (char *, nb_scattering);
2035   name_tree p;
2036
2037   for (i = 0; VEC_iterate (name_tree, SCOP_PARAMS (scop), i, p); i++)
2038     save_var_name (params, i, p);
2039
2040   cloog_names_set_nb_parameters (cloog_program_names (SCOP_PROG (scop)),
2041                                  nb_params);
2042   cloog_names_set_parameters (cloog_program_names (SCOP_PROG (scop)),
2043                               params);
2044
2045   for (i = 0; i < nb_iterators; i++)
2046     {
2047       iterators[i] = XNEWVEC (char, 18 + 12);
2048       sprintf (iterators[i], "graphite_iterator_%d", i);
2049     }
2050
2051   cloog_names_set_nb_iterators (cloog_program_names (SCOP_PROG (scop)),
2052                                 nb_iterators);
2053   cloog_names_set_iterators (cloog_program_names (SCOP_PROG (scop)),
2054                              iterators);
2055
2056   for (i = 0; i < nb_scattering; i++)
2057     {
2058       scattering[i] = XNEWVEC (char, 2 + 12);
2059       sprintf (scattering[i], "s_%d", i);
2060     }
2061
2062   cloog_names_set_nb_scattering (cloog_program_names (SCOP_PROG (scop)),
2063                                  nb_scattering);
2064   cloog_names_set_scattering (cloog_program_names (SCOP_PROG (scop)),
2065                               scattering);
2066 }
2067
2068 /* Record the parameters used in the SCOP.  A variable is a parameter
2069    in a scop if it does not vary during the execution of that scop.  */
2070
2071 static void
2072 find_scop_parameters (scop_p scop)
2073 {
2074   graphite_bb_p gb;
2075   unsigned i;
2076   struct loop *loop;
2077   Value one;
2078
2079   value_init (one);
2080   value_set_si (one, 1);
2081
2082   /* Find the parameters used in the loop bounds.  */
2083   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop); i++)
2084     {
2085       tree nb_iters = number_of_latch_executions (loop);
2086
2087       if (!chrec_contains_symbols (nb_iters))
2088         continue;
2089
2090       nb_iters = analyze_scalar_evolution (loop, nb_iters);
2091       nb_iters = instantiate_scev (block_before_scop (scop), loop, nb_iters);
2092       scan_tree_for_params (scop, nb_iters, NULL, 0, one, false);
2093     }
2094
2095   value_clear (one);
2096
2097   /* Find the parameters used in data accesses.  */
2098   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
2099     find_params_in_bb (scop, GBB_BB (gb));
2100 }
2101
2102 /* Build the context constraints for SCOP: constraints and relations
2103    on parameters.  */
2104
2105 static void
2106 build_scop_context (scop_p scop)
2107 {
2108   int nb_params = scop_nb_params (scop);
2109   CloogMatrix *matrix = cloog_matrix_alloc (1, nb_params + 2);
2110
2111   /* Insert '0 >= 0' in the context matrix, as it is not allowed to be
2112      empty. */
2113  
2114   value_set_si (matrix->p[0][0], 1);
2115
2116   value_set_si (matrix->p[0][nb_params + 1], 0);
2117
2118   cloog_program_set_context (SCOP_PROG (scop),
2119                              cloog_domain_matrix2domain (matrix));
2120   cloog_matrix_free (matrix);
2121 }
2122
2123 /* Returns a graphite_bb from BB.  */
2124
2125 static inline graphite_bb_p
2126 gbb_from_bb (basic_block bb)
2127 {
2128   return (graphite_bb_p) bb->aux;
2129 }
2130
2131 /* Add DOMAIN to all the basic blocks in LOOP.  */
2132
2133 static void
2134 add_bb_domains (struct loop *loop, CloogMatrix *domain)
2135 {
2136   basic_block *bbs = get_loop_body (loop);
2137   unsigned i;
2138
2139   for (i = 0; i < loop->num_nodes; i++)
2140     if (bbs[i]->loop_father == loop)
2141       {
2142         graphite_bb_p gbb = gbb_from_bb (bbs[i]);
2143         GBB_DOMAIN (gbb) = cloog_matrix_copy (domain);
2144       }
2145
2146   free (bbs);
2147 }
2148
2149 /* Builds the constraint matrix for LOOP in SCOP.  NB_OUTER_LOOPS is the
2150    number of loops surrounding LOOP in SCOP.  OUTER_CSTR gives the
2151    constraints matrix for the surrounding loops.  */
2152
2153 static void
2154 build_loop_iteration_domains (scop_p scop, struct loop *loop,
2155                               CloogMatrix *outer_cstr, int nb_outer_loops)
2156 {
2157   int i, j, row;
2158   CloogMatrix *cstr;
2159
2160   int nb_rows = outer_cstr->NbRows + 1;
2161   int nb_cols = outer_cstr->NbColumns + 1;
2162
2163   /* Last column of CSTR is the column of constants.  */
2164   int cst_col = nb_cols - 1;
2165
2166   /* The column for the current loop is just after the columns of
2167      other outer loops.  */
2168   int loop_col = nb_outer_loops + 1;
2169
2170   tree nb_iters = number_of_latch_executions (loop);
2171
2172   /* When the number of iterations is a constant or a parameter, we
2173      add a constraint for the upper bound of the loop.  So add a row
2174      to the constraint matrix before allocating it.  */
2175   if (TREE_CODE (nb_iters) == INTEGER_CST
2176       || !chrec_contains_undetermined (nb_iters))
2177     nb_rows++;
2178
2179   cstr = cloog_matrix_alloc (nb_rows, nb_cols);
2180
2181   /* Copy the outer constraints.  */
2182   for (i = 0; i < outer_cstr->NbRows; i++)
2183     {
2184       /* Copy the eq/ineq and loops columns.  */
2185       for (j = 0; j < loop_col; j++)
2186         value_assign (cstr->p[i][j], outer_cstr->p[i][j]);
2187
2188       /* Leave an empty column in CSTR for the current loop, and then
2189         copy the parameter columns.  */
2190       for (j = loop_col; j < outer_cstr->NbColumns; j++)
2191         value_assign (cstr->p[i][j + 1], outer_cstr->p[i][j]);
2192     }
2193
2194   /* 0 <= loop_i */
2195   row = outer_cstr->NbRows;
2196   value_set_si (cstr->p[row][0], 1);
2197   value_set_si (cstr->p[row][loop_col], 1);
2198
2199   /* loop_i <= nb_iters */
2200   if (TREE_CODE (nb_iters) == INTEGER_CST)
2201     {
2202       row++;
2203       value_set_si (cstr->p[row][0], 1);
2204       value_set_si (cstr->p[row][loop_col], -1);
2205
2206       value_set_si (cstr->p[row][cst_col],
2207                     int_cst_value (nb_iters));
2208     }
2209   else if (!chrec_contains_undetermined (nb_iters))
2210     {
2211       /* Otherwise nb_iters contains parameters: scan the nb_iters
2212          expression and build its matrix representation.  */
2213       Value one;
2214
2215       row++;
2216       value_set_si (cstr->p[row][0], 1);
2217       value_set_si (cstr->p[row][loop_col], -1);
2218
2219       nb_iters = analyze_scalar_evolution (loop, nb_iters);
2220       nb_iters = instantiate_scev (block_before_scop (scop), loop, nb_iters);
2221
2222       value_init (one);
2223       value_set_si (one, 1);
2224       scan_tree_for_params (scop, nb_iters, cstr, row, one, false);
2225       value_clear (one);
2226     }
2227   else
2228     gcc_unreachable ();
2229
2230   if (loop->inner && loop_in_scop_p (loop->inner, scop))
2231     build_loop_iteration_domains (scop, loop->inner, cstr, nb_outer_loops + 1);
2232
2233   /* Only go to the next loops, if we are not at the outermost layer.  These
2234      have to be handled seperately, as we can be sure, that the chain at this
2235      layer will be connected.  */
2236   if (nb_outer_loops != 0 && loop->next && loop_in_scop_p (loop->next, scop))
2237     build_loop_iteration_domains (scop, loop->next, outer_cstr, nb_outer_loops);
2238
2239   add_bb_domains (loop, cstr);
2240
2241   cloog_matrix_free (cstr);
2242 }
2243
2244 /* Add conditions to the domain of GB.  */
2245
2246 static void
2247 add_conditions_to_domain (graphite_bb_p gb)
2248 {
2249   unsigned int i,j;
2250   gimple stmt;
2251   VEC (gimple, heap) *conditions = GBB_CONDITIONS (gb);
2252   CloogMatrix *domain = GBB_DOMAIN (gb);
2253   scop_p scop = GBB_SCOP (gb);
2254
2255   unsigned nb_rows;
2256   unsigned nb_cols;
2257   unsigned nb_new_rows = 0;
2258   unsigned row;
2259
2260   if (VEC_empty (gimple, conditions))
2261     return;
2262
2263   if (domain)
2264     {
2265       nb_rows = domain->NbRows;
2266       nb_cols = domain->NbColumns;
2267     }
2268   else  
2269     {
2270       nb_rows = 0;
2271       nb_cols = scop_nb_params (scop) + 2;
2272     }
2273
2274   /* Count number of necessary new rows to add the conditions to the
2275      domain.  */
2276   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
2277     {
2278       switch (gimple_code (stmt))
2279         {
2280         case GIMPLE_COND:
2281           {
2282             enum tree_code code = gimple_cond_code (stmt);
2283
2284             switch (code)
2285               {
2286               case NE_EXPR:
2287               case EQ_EXPR:
2288                 /* NE and EQ statements are not supported right know. */
2289                 gcc_unreachable ();
2290                 break;
2291               case LT_EXPR:
2292               case GT_EXPR:
2293               case LE_EXPR:
2294               case GE_EXPR:
2295                 nb_new_rows++;
2296                 break;
2297               default:
2298                 gcc_unreachable ();
2299                 break;
2300               }
2301           break;
2302           }
2303         case SWITCH_EXPR:
2304           /* Switch statements are not supported right know.  */
2305           gcc_unreachable ();
2306           break;
2307
2308         default:
2309           gcc_unreachable ();
2310           break;
2311         }
2312     }
2313
2314
2315   /* Enlarge the matrix.  */ 
2316   { 
2317     CloogMatrix *new_domain;
2318     new_domain = cloog_matrix_alloc (nb_rows + nb_new_rows, nb_cols);
2319
2320     for (i = 0; i < nb_rows; i++)
2321       for (j = 0; j < nb_cols; j++)
2322           value_assign (new_domain->p[i][j], domain->p[i][j]);
2323
2324     cloog_matrix_free (domain);
2325     domain = new_domain;
2326     GBB_DOMAIN (gb) = new_domain;
2327   }     
2328
2329   /* Add the conditions to the new enlarged domain matrix.  */
2330   row = nb_rows;
2331   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
2332     {
2333       switch (gimple_code (stmt))
2334         {
2335         case GIMPLE_COND:
2336           {
2337             Value one;
2338             enum tree_code code;
2339             tree left;
2340             tree right;
2341             loop_p loop = GBB_BB (gb)->loop_father;
2342
2343             left = gimple_cond_lhs (stmt);
2344             right = gimple_cond_rhs (stmt);
2345
2346             left = analyze_scalar_evolution (loop, left);
2347             right = analyze_scalar_evolution (loop, right);
2348
2349             left = instantiate_scev (block_before_scop (scop), loop, left);
2350             right = instantiate_scev (block_before_scop (scop), loop, right);
2351
2352             code = gimple_cond_code (stmt);
2353
2354             /* The conditions for ELSE-branches are inverted.  */
2355             if (VEC_index (gimple, gb->condition_cases, i) == NULL)
2356               code = invert_tree_comparison (code, false);
2357
2358             switch (code)
2359               {
2360               case NE_EXPR:
2361                 /* NE statements are not supported right know. */
2362                 gcc_unreachable ();
2363                 break;
2364               case EQ_EXPR:
2365                 value_set_si (domain->p[row][0], 1);
2366                 value_init (one);
2367                 value_set_si (one, 1);
2368                 scan_tree_for_params (scop, left, domain, row, one, true);
2369                 value_set_si (one, 1);
2370                 scan_tree_for_params (scop, right, domain, row, one, false);
2371                 row++;
2372                 value_set_si (domain->p[row][0], 1);
2373                 value_set_si (one, 1);
2374                 scan_tree_for_params (scop, left, domain, row, one, false);
2375                 value_set_si (one, 1);
2376                 scan_tree_for_params (scop, right, domain, row, one, true);
2377                 value_clear (one);
2378                 row++;
2379                 break;
2380               case LT_EXPR:
2381                 value_set_si (domain->p[row][0], 1);
2382                 value_init (one);
2383                 value_set_si (one, 1);
2384                 scan_tree_for_params (scop, left, domain, row, one, true);
2385                 value_set_si (one, 1);
2386                 scan_tree_for_params (scop, right, domain, row, one, false);
2387                 value_sub_int (domain->p[row][nb_cols - 1],
2388                     domain->p[row][nb_cols - 1], 1); 
2389                 value_clear (one);
2390                 row++;
2391                 break;
2392               case GT_EXPR:
2393                 value_set_si (domain->p[row][0], 1);
2394                 value_init (one);
2395                 value_set_si (one, 1);
2396                 scan_tree_for_params (scop, left, domain, row, one, false);
2397                 value_set_si (one, 1);
2398                 scan_tree_for_params (scop, right, domain, row, one, true);
2399                 value_sub_int (domain->p[row][nb_cols - 1],
2400                     domain->p[row][nb_cols - 1], 1);
2401                 value_clear (one);
2402                 row++;
2403                 break;
2404               case LE_EXPR:
2405                 value_set_si (domain->p[row][0], 1);
2406                 value_init (one);
2407                 value_set_si (one, 1);
2408                 scan_tree_for_params (scop, left, domain, row, one, true);
2409                 value_set_si (one, 1);
2410                 scan_tree_for_params (scop, right, domain, row, one, false);
2411                 value_clear (one);
2412                 row++;
2413                 break;
2414               case GE_EXPR:
2415                 value_set_si (domain->p[row][0], 1);
2416                 value_init (one);
2417                 value_set_si (one, 1);
2418                 scan_tree_for_params (scop, left, domain, row, one, false);
2419                 value_set_si (one, 1);
2420                 scan_tree_for_params (scop, right, domain, row, one, true);
2421                 value_clear (one);
2422                 row++;
2423                 break;
2424               default:
2425                 gcc_unreachable ();
2426                 break;
2427               }
2428             break;
2429           }
2430         case GIMPLE_SWITCH:
2431           /* Switch statements are not supported right know.  */
2432           gcc_unreachable ();
2433           break;
2434
2435         default:
2436           gcc_unreachable ();
2437           break;
2438         }
2439     }
2440 }
2441
2442 /* Helper recursive function.  */
2443
2444 static void
2445 build_scop_conditions_1 (VEC (gimple, heap) **conditions,
2446                          VEC (gimple, heap) **cases, basic_block bb,
2447                          scop_p scop)
2448 {
2449   int i, j;
2450   graphite_bb_p gbb;
2451   gimple_stmt_iterator gsi;
2452   basic_block bb_child, bb_iter;
2453   VEC (basic_block, heap) *dom;
2454   
2455   /* Make sure we are in the SCoP.  */
2456   if (!bb_in_scop_p (bb, scop))
2457     return;
2458
2459   /* Record conditions in graphite_bb.  */
2460   gbb = gbb_from_bb (bb);
2461   GBB_CONDITIONS (gbb) = VEC_copy (gimple, heap, *conditions);
2462   GBB_CONDITION_CASES (gbb) = VEC_copy (gimple, heap, *cases);
2463
2464   add_conditions_to_domain (gbb);
2465
2466   dom = get_dominated_by (CDI_DOMINATORS, bb);
2467
2468   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2469     {
2470       gimple stmt = gsi_stmt (gsi);
2471       VEC (edge, gc) *edges;
2472       edge e;
2473
2474       switch (gimple_code (stmt))
2475         {
2476         case GIMPLE_COND:
2477           edges = bb->succs;
2478           for (i = 0; VEC_iterate (edge, edges, i, e); i++)
2479             if ((dominated_by_p (CDI_DOMINATORS, e->dest, bb))
2480                 && VEC_length (edge, e->dest->preds) == 1)
2481               {
2482                 /* Remove the scanned block from the dominator successors.  */
2483                 for (j = 0; VEC_iterate (basic_block, dom, j, bb_iter); j++)
2484                   if (bb_iter == e->dest)
2485                     {
2486                       VEC_unordered_remove (basic_block, dom, j);
2487                       break;
2488                     }
2489
2490                 /* Recursively scan the then or else part.  */
2491                 if (e->flags & EDGE_TRUE_VALUE)
2492                   VEC_safe_push (gimple, heap, *cases, stmt);
2493                 else if (e->flags & EDGE_FALSE_VALUE)
2494                   VEC_safe_push (gimple, heap, *cases, NULL);
2495                 else
2496                   gcc_unreachable ();
2497
2498                 VEC_safe_push (gimple, heap, *conditions, stmt);
2499                 build_scop_conditions_1 (conditions, cases, e->dest, scop);
2500                 VEC_pop (gimple, *conditions);
2501                 VEC_pop (gimple, *cases);
2502               }
2503           break;
2504
2505         case GIMPLE_SWITCH:
2506           {
2507             unsigned i;
2508             gimple_stmt_iterator gsi_search_gimple_label;
2509
2510             for (i = 0; i < gimple_switch_num_labels (stmt); ++i)
2511               {
2512                 basic_block bb_iter;
2513                 size_t k;
2514                 size_t n_cases = VEC_length (gimple, *conditions);
2515                 unsigned n = gimple_switch_num_labels (stmt);
2516
2517                 bb_child = label_to_block
2518                   (CASE_LABEL (gimple_switch_label (stmt, i)));
2519
2520                 /* Do not handle multiple values for the same block.  */
2521                 for (k = 0; k < n; k++)
2522                   if (i != k
2523                       && label_to_block 
2524                       (CASE_LABEL (gimple_switch_label (stmt, k))) == bb_child)
2525                     break;
2526
2527                 if (k != n)
2528                   continue;
2529
2530                 /* Switch cases with more than one predecessor are not
2531                    handled.  */
2532                 if (VEC_length (edge, bb_child->preds) != 1)
2533                   continue;
2534
2535                 /* Recursively scan the corresponding 'case' block.  */
2536
2537                 for (gsi_search_gimple_label = gsi_start_bb (bb_child);
2538                      !gsi_end_p (gsi_search_gimple_label);
2539                      gsi_next (&gsi_search_gimple_label))
2540                   {
2541                     gimple stmt_gimple_label 
2542                       = gsi_stmt (gsi_search_gimple_label);
2543
2544                     if (gimple_code (stmt_gimple_label) == GIMPLE_LABEL)
2545                       {
2546                         tree t = gimple_label_label (stmt_gimple_label);
2547
2548                         if (t == gimple_switch_label (stmt, i))
2549                           VEC_replace (gimple, *cases, n_cases,
2550                                        stmt_gimple_label);
2551                         else
2552                           gcc_unreachable ();
2553                       }
2554                   }
2555
2556                 build_scop_conditions_1 (conditions, cases, bb_child, scop);
2557
2558                 /* Remove the scanned block from the dominator successors.  */
2559                 for (j = 0; VEC_iterate (basic_block, dom, j, bb_iter); j++)
2560                   if (bb_iter == bb_child)
2561                     {
2562                       VEC_unordered_remove (basic_block, dom, j);
2563                       break;
2564                     }  
2565               }
2566
2567             VEC_pop (gimple, *conditions);
2568             VEC_pop (gimple, *cases);
2569             break;
2570           }
2571         default:
2572           break;
2573       }
2574   }
2575
2576   /* Scan all immediate dominated successors.  */
2577   for (i = 0; VEC_iterate (basic_block, dom, i, bb_child); i++)
2578     build_scop_conditions_1 (conditions, cases, bb_child, scop);
2579
2580   VEC_free (basic_block, heap, dom);
2581 }
2582
2583 /* Record all 'if' and 'switch' conditions in each gbb of SCOP.  */
2584
2585 static void
2586 build_scop_conditions (scop_p scop)
2587 {
2588   VEC (gimple, heap) *conditions = NULL;
2589   VEC (gimple, heap) *cases = NULL;
2590
2591   build_scop_conditions_1 (&conditions, &cases, SCOP_ENTRY (scop), scop);
2592
2593   VEC_free (gimple, heap, conditions);
2594   VEC_free (gimple, heap, cases);
2595 }
2596
2597 /* Build the current domain matrix: the loops belonging to the current
2598    SCOP, and that vary for the execution of the current basic block.
2599    Returns false if there is no loop in SCOP.  */
2600
2601 static bool
2602 build_scop_iteration_domain (scop_p scop)
2603 {
2604   struct loop *loop;
2605   CloogMatrix *outer_cstr;
2606   int i;
2607
2608   /* Build cloog loop for all loops, that are in the uppermost loop layer of
2609      this SCoP.  */
2610   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop); i++)
2611     if (!loop_in_scop_p (loop_outer (loop), scop))
2612       {
2613         /* The outermost constraints is a matrix that has:
2614            -first column: eq/ineq boolean
2615            -last column: a constant
2616            -scop_nb_params columns for the parameters used in the scop.  */
2617        outer_cstr = cloog_matrix_alloc (0, scop_nb_params (scop) + 2);
2618        build_loop_iteration_domains (scop, loop, outer_cstr, 0);
2619        cloog_matrix_free (outer_cstr);
2620      }
2621
2622   return (i != 0);
2623 }
2624
2625 /* Initializes an equation CY of the access matrix using the
2626    information for a subscript from ACCESS_FUN, relatively to the loop
2627    indexes from LOOP_NEST and parameter indexes from PARAMS.  NDIM is
2628    the dimension of the array access, i.e. the number of
2629    subscripts.  Returns true when the operation succeeds.  */
2630
2631 static bool
2632 build_access_matrix_with_af (tree access_fun, lambda_vector cy,
2633                              scop_p scop, int ndim)
2634 {
2635   switch (TREE_CODE (access_fun))
2636     {
2637     case POLYNOMIAL_CHREC:
2638       {
2639         tree left = CHREC_LEFT (access_fun);
2640         tree right = CHREC_RIGHT (access_fun);
2641         int var;
2642
2643         if (TREE_CODE (right) != INTEGER_CST)
2644           return false;
2645         
2646         var = loop_iteration_vector_dim (CHREC_VARIABLE (access_fun), scop);
2647         cy[var] = int_cst_value (right);
2648
2649         switch (TREE_CODE (left))
2650           {
2651           case POLYNOMIAL_CHREC:
2652             return build_access_matrix_with_af (left, cy, scop, ndim);
2653
2654           case INTEGER_CST:
2655             cy[ndim - 1] = int_cst_value (left);
2656             return true;
2657
2658           default:
2659             /* FIXME: access_fn can have parameters.  */
2660             return false;
2661           }
2662       }
2663     case INTEGER_CST:
2664       cy[ndim - 1] = int_cst_value (access_fun);
2665       return true;
2666
2667     default:
2668       /* FIXME: access_fn can have parameters.  */
2669       return false;
2670     }
2671 }
2672
2673 /* Initialize the access matrix in the data reference REF with respect
2674    to the loop nesting LOOP_NEST.  Return true when the operation
2675    succeeded.  */
2676
2677 static bool
2678 build_access_matrix (data_reference_p ref, graphite_bb_p gb)
2679 {
2680   int i, ndim = DR_NUM_DIMENSIONS (ref);
2681   struct access_matrix *am = GGC_NEW (struct access_matrix);
2682
2683   AM_MATRIX (am) = VEC_alloc (lambda_vector, heap, ndim);
2684   DR_SCOP (ref) = GBB_SCOP (gb);
2685
2686   for (i = 0; i < ndim; i++)
2687     {
2688       lambda_vector v = lambda_vector_new (ref_nb_loops (ref));
2689       scop_p scop = GBB_SCOP (gb);
2690       tree af = DR_ACCESS_FN (ref, i);
2691
2692       if (!build_access_matrix_with_af (af, v, scop, ref_nb_loops (ref)))
2693         return false;
2694
2695       VEC_safe_push (lambda_vector, heap, AM_MATRIX (am), v);
2696     }
2697
2698   DR_ACCESS_MATRIX (ref) = am;
2699   return true;
2700 }
2701
2702 /* Build the access matrices for the data references in the SCOP.  */
2703
2704 static void
2705 build_scop_data_accesses (scop_p scop)
2706 {
2707   int i;
2708   graphite_bb_p gb;
2709
2710   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
2711     {
2712       int j;
2713       gimple_stmt_iterator gsi;
2714       data_reference_p dr;
2715       struct loop *nest = outermost_loop_in_scop (scop, GBB_BB (gb));
2716
2717       /* On each statement of the basic block, gather all the occurences
2718          to read/write memory.  */
2719       GBB_DATA_REFS (gb) = VEC_alloc (data_reference_p, heap, 5);
2720       for (gsi = gsi_start_bb (GBB_BB (gb)); !gsi_end_p (gsi); gsi_next (&gsi))
2721         find_data_references_in_stmt (nest, gsi_stmt (gsi),
2722                                       &GBB_DATA_REFS (gb));
2723
2724       /* FIXME: Construction of access matrix is disabled until some
2725          pass, like the data dependence analysis, is using it.  */
2726       continue;
2727
2728       /* Construct the access matrix for each data ref, with respect to
2729          the loop nest of the current BB in the considered SCOP.  */
2730       for (j = 0;
2731            VEC_iterate (data_reference_p, GBB_DATA_REFS (gb), j, dr);
2732            j++)
2733         {
2734           bool res = build_access_matrix (dr, gb);
2735
2736           /* FIXME: At this point the DRs should always have an affine
2737              form.  For the moment this fails as build_access_matrix
2738              does not build matrices with parameters.  */
2739           gcc_assert (res);
2740         }
2741     }
2742 }
2743
2744 /* Converts a GMP constant value to a tree and returns it.  */
2745
2746 static tree
2747 gmp_cst_to_tree (Value v)
2748 {
2749   return build_int_cst (integer_type_node, value_get_si (v));
2750 }
2751
2752 /* Returns the tree variable from the name NAME that was given in
2753    Cloog representation.  All the parameters are stored in PARAMS, and
2754    all the loop induction variables are stored in IVSTACK.
2755
2756    FIXME: This is a hack, and Cloog should be fixed to not work with
2757    variable names represented as "char *string", but with void
2758    pointers that could be casted back to a tree.  The only problem in
2759    doing that is that Cloog's pretty printer still assumes that
2760    variable names are char *strings.  The solution would be to have a
2761    function pointer for pretty-printing that can be redirected to be
2762    print_generic_stmt in our case, or fprintf by default.
2763    ???  Too ugly to live.  */
2764
2765 static tree
2766 clast_name_to_gcc (const char *name, VEC (name_tree, heap) *params, 
2767                    loop_iv_stack ivstack)
2768 {
2769   int i;
2770   name_tree t;
2771   tree iv;
2772
2773   for (i = 0; VEC_iterate (name_tree, params, i, t); i++)
2774     if (!strcmp (name, t->name))
2775       return t->t;
2776
2777   iv = loop_iv_stack_get_iv_from_name (ivstack, name);
2778   if (iv)
2779     return iv;
2780
2781   gcc_unreachable ();
2782 }
2783
2784 /* Converts a Cloog AST expression E back to a GCC expression tree.   */
2785
2786 static tree
2787 clast_to_gcc_expression (struct clast_expr *e,
2788                          VEC (name_tree, heap) *params,
2789                          loop_iv_stack ivstack)
2790 {
2791   tree type = integer_type_node;
2792
2793   gcc_assert (e);
2794
2795   switch (e->type)
2796     {
2797     case expr_term:
2798       {
2799         struct clast_term *t = (struct clast_term *) e;
2800
2801         if (t->var)
2802           {
2803             if (value_one_p (t->val))
2804               return clast_name_to_gcc (t->var, params, ivstack);
2805
2806             else if (value_mone_p (t->val))
2807               return fold_build1 (NEGATE_EXPR, type,
2808                                   clast_name_to_gcc (t->var, params, ivstack));
2809             else
2810               return fold_build2 (MULT_EXPR, type,
2811                                   gmp_cst_to_tree (t->val),
2812                                   clast_name_to_gcc (t->var, params, ivstack));
2813           }
2814         else
2815           return gmp_cst_to_tree (t->val);
2816       }
2817
2818     case expr_red:
2819       {
2820         struct clast_reduction *r = (struct clast_reduction *) e;
2821         tree left, right;
2822
2823         switch (r->type)
2824           {
2825           case clast_red_sum:
2826             if (r->n == 1)
2827               return clast_to_gcc_expression (r->elts[0], params, ivstack);
2828
2829             else 
2830               {
2831                 gcc_assert (r->n >= 1
2832                             && r->elts[0]->type == expr_term
2833                             && r->elts[1]->type == expr_term);
2834
2835                 left = clast_to_gcc_expression (r->elts[0], params, ivstack);
2836                 right = clast_to_gcc_expression (r->elts[1], params, ivstack);
2837                 return fold_build2 (PLUS_EXPR, type, left, right);
2838               }
2839
2840             break;
2841
2842           case clast_red_min:
2843             if (r->n == 1)
2844               return clast_to_gcc_expression (r->elts[0], params, ivstack);
2845
2846             else if (r->n == 2)
2847               {
2848                 left = clast_to_gcc_expression (r->elts[0], params, ivstack);
2849                 right = clast_to_gcc_expression (r->elts[1], params, ivstack);
2850                 return fold_build2 (MIN_EXPR, type, left, right);
2851               }
2852
2853             else
2854               gcc_unreachable();
2855
2856             break;
2857
2858           case clast_red_max:
2859             if (r->n == 1)
2860               return clast_to_gcc_expression (r->elts[0], params, ivstack);
2861
2862             else if (r->n == 2)
2863               {
2864                 left = clast_to_gcc_expression (r->elts[0], params, ivstack);
2865                 right = clast_to_gcc_expression (r->elts[1], params, ivstack);
2866                 return fold_build2 (MAX_EXPR, type, left, right);
2867               }
2868
2869             else
2870               gcc_unreachable();
2871
2872             break;
2873
2874           default:
2875             gcc_unreachable ();
2876           }
2877         break;
2878       }
2879
2880     case expr_bin:
2881       {
2882         struct clast_binary *b = (struct clast_binary *) e;
2883         struct clast_expr *lhs = (struct clast_expr *) b->LHS;
2884         struct clast_expr *rhs = (struct clast_expr *) b->RHS;
2885         tree tl = clast_to_gcc_expression (lhs, params, ivstack);
2886
2887         /* FIXME: The next statement produces a warning: Cloog assumes
2888            that the RHS is a constant, but this is a "void *" pointer
2889            that should be casted into a Value, but this cast cannot be
2890            done as Value is a GMP type, that is an array.  Cloog must
2891            be fixed for removing this warning.  */
2892         tree tr = gmp_cst_to_tree (rhs);
2893
2894         switch (b->type)
2895           {
2896           case clast_bin_fdiv:
2897             return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
2898
2899           case clast_bin_cdiv:
2900             return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
2901
2902           case clast_bin_div:
2903             return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
2904
2905           case clast_bin_mod:
2906             return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
2907
2908           default:
2909             gcc_unreachable ();
2910           }
2911       }
2912
2913     default:
2914       gcc_unreachable ();
2915     }
2916
2917   return NULL_TREE;
2918 }
2919
2920 /* Translates a clast equation CLEQ to a tree.  */
2921
2922 static tree
2923 graphite_translate_clast_equation (scop_p scop,
2924                                    struct clast_equation *cleq,
2925                                    loop_iv_stack ivstack)
2926 {
2927   enum tree_code comp;
2928   tree lhs = clast_to_gcc_expression (cleq->LHS, SCOP_PARAMS (scop), ivstack);
2929   tree rhs = clast_to_gcc_expression (cleq->RHS, SCOP_PARAMS (scop), ivstack);
2930
2931   if (cleq->sign == 0)
2932     comp = EQ_EXPR;
2933
2934   else if (cleq->sign > 0)
2935     comp = GE_EXPR;
2936
2937   else
2938     comp = LE_EXPR;
2939
2940   return fold_build2 (comp, integer_type_node, lhs, rhs);
2941 }
2942
2943 /* Creates the test for the condition in STMT.  */
2944
2945 static tree
2946 graphite_create_guard_cond_expr (scop_p scop, struct clast_guard *stmt, 
2947                                  loop_iv_stack ivstack)
2948 {
2949   tree cond = NULL;
2950   int i;
2951
2952   for (i = 0; i < stmt->n; i++)
2953     {
2954       tree eq = graphite_translate_clast_equation (scop, &stmt->eq[i], ivstack);
2955
2956       if (cond)
2957         cond = fold_build2 (TRUTH_AND_EXPR, integer_type_node, cond, eq);
2958       else
2959         cond = eq;
2960     }
2961
2962   return cond;
2963 }
2964
2965 /* Creates a new if region corresponding to Cloog's guard.  */
2966
2967 static edge 
2968 graphite_create_new_guard (scop_p scop, edge entry_edge,
2969                            struct clast_guard *stmt, 
2970                            loop_iv_stack ivstack)
2971 {
2972   tree cond_expr = graphite_create_guard_cond_expr (scop, stmt, ivstack);
2973   edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
2974   return exit_edge;
2975 }
2976
2977
2978 /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an induction 
2979    variable for the new LOOP.  New LOOP is attached to CFG starting at
2980    ENTRY_EDGE.  LOOP is inserted into the loop tree and becomes the child
2981    loop of the OUTER_LOOP.  */
2982
2983 static struct loop *
2984 graphite_create_new_loop (scop_p scop, edge entry_edge,
2985                           struct clast_for *stmt, loop_iv_stack ivstack,
2986                           loop_p outer)
2987 {
2988   struct loop *loop;
2989   tree ivvar;
2990   tree stride, lowb, upb;
2991   tree iv_before;
2992
2993   gcc_assert (stmt->LB
2994               && stmt->UB);
2995
2996   stride = gmp_cst_to_tree (stmt->stride);
2997   lowb = clast_to_gcc_expression (stmt->LB, SCOP_PARAMS (scop), ivstack);
2998   ivvar = create_tmp_var (integer_type_node, "graphiteIV");
2999   add_referenced_var (ivvar);
3000
3001   upb = clast_to_gcc_expression (stmt->UB, SCOP_PARAMS (scop), ivstack);
3002   loop = create_empty_loop_on_edge (entry_edge, lowb, stride, upb, ivvar,
3003                                     &iv_before, outer ? outer
3004                                     : entry_edge->src->loop_father);
3005
3006   loop_iv_stack_push (ivstack, iv_before, stmt->iterator);
3007
3008   return loop;
3009 }
3010
3011 /* Remove all the edges from EDGES except the edge KEEP.  */
3012
3013 static void
3014 remove_all_edges_1 (VEC (edge, gc) *edges, edge keep)
3015 {
3016   edge e;
3017   edge_iterator ei;
3018
3019   for (ei = ei_start (edges); (e = ei_safe_edge (ei)); )
3020     {
3021       if (e != keep)
3022         {
3023           remove_edge (e);
3024           e = ei_safe_edge (ei);
3025         }
3026       else
3027         ei_next (&ei);
3028     }
3029 }
3030
3031 /* Remove all the edges from BB except the edge KEEP.  */
3032
3033 static void
3034 remove_all_edges (basic_block bb, edge keep)
3035 {
3036   remove_all_edges_1 (bb->succs, keep);
3037   remove_all_edges_1 (bb->preds, keep);
3038 }
3039
3040 /* Rename the SSA_NAMEs used in STMT and that appear in IVSTACK.  */
3041
3042 static void 
3043 graphite_rename_ivs_stmt (gimple stmt, graphite_bb_p gbb, scop_p scop,
3044                           loop_p old, loop_iv_stack ivstack)
3045 {
3046   ssa_op_iter iter;
3047   use_operand_p use_p;
3048
3049   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3050     {
3051       tree use = USE_FROM_PTR (use_p);
3052       tree new_iv = NULL;
3053       name_tree old_iv = get_old_iv_from_ssa_name (scop, old, use);
3054       
3055       if (old_iv)
3056         new_iv = loop_iv_stack_get_iv (ivstack,
3057                                        gbb_loop_index (gbb, old_iv->loop));
3058
3059       if (new_iv)
3060         SET_USE (use_p, new_iv);
3061     }
3062 }
3063
3064 /* Returns true if SSA_NAME is a parameter of SCOP.  */
3065
3066 static bool
3067 is_parameter (scop_p scop, tree ssa_name)
3068 {
3069   int i;
3070   VEC (name_tree, heap) *params = SCOP_PARAMS (scop);
3071   name_tree param;
3072
3073   for (i = 0; VEC_iterate (name_tree, params, i, param); i++)
3074     if (param->t == ssa_name)
3075       return true;
3076
3077   return false;
3078 }
3079
3080 /* Returns true if NAME is an old induction variable in SCOP.  OLD is
3081    the original loop that contained the definition of NAME.  */
3082
3083 static bool
3084 is_old_iv (scop_p scop, loop_p old, tree name)
3085 {
3086   return get_old_iv_from_ssa_name (scop, old, name) != NULL;
3087
3088 }
3089
3090 static void expand_scalar_variables_stmt (gimple, graphite_bb_p, scop_p, loop_p,
3091                                           loop_iv_stack);
3092
3093 /* Constructs a tree which only contains old_ivs and parameters.  Any
3094    other variables that are defined outside GBB will be eliminated by
3095    using their definitions in the constructed tree.  OLD_LOOP_FATHER
3096    is the original loop that contained GBB.  */
3097
3098 static tree
3099 expand_scalar_variables_expr (tree type, tree op0, enum tree_code code, 
3100                               tree op1, graphite_bb_p gbb, scop_p scop, 
3101                               loop_p old_loop_father, loop_iv_stack ivstack)
3102 {
3103   if (TREE_CODE_CLASS (code) == tcc_constant
3104       && code == INTEGER_CST)
3105       return op0;
3106
3107   if (TREE_CODE_CLASS (code) == tcc_unary)
3108     {
3109       tree op0_type = TREE_TYPE (op0);
3110       enum tree_code op0_code = TREE_CODE (op0);
3111       tree op0_expr = 
3112         expand_scalar_variables_expr (op0_type, op0, op0_code,
3113                                       NULL, gbb, scop, old_loop_father,
3114                                       ivstack);
3115
3116       return fold_build1 (code, type, op0_expr);
3117     }
3118
3119   if (TREE_CODE_CLASS (code) == tcc_binary)
3120     {
3121       tree op0_type = TREE_TYPE (op0);
3122       enum tree_code op0_code = TREE_CODE (op0);
3123       tree op0_expr = 
3124         expand_scalar_variables_expr (op0_type, op0, op0_code,
3125                                       NULL, gbb, scop, old_loop_father,
3126                                       ivstack);
3127       tree op1_type = TREE_TYPE (op1);
3128       enum tree_code op1_code = TREE_CODE (op1);
3129       tree op1_expr = 
3130         expand_scalar_variables_expr (op1_type, op1, op1_code,
3131                                       NULL, gbb, scop, old_loop_father,
3132                                       ivstack);
3133
3134       return fold_build2 (code, type, op0_expr, op1_expr);
3135     }
3136
3137   if (code == SSA_NAME)
3138     {
3139       tree var0, var1;
3140       gimple def_stmt;
3141       enum tree_code subcode;
3142       
3143       if(is_parameter (scop, op0) ||
3144          is_old_iv (scop, old_loop_father, op0))
3145         return op0;
3146       
3147       def_stmt = SSA_NAME_DEF_STMT (op0);
3148       
3149       if (gimple_bb (def_stmt) == GBB_BB (gbb))
3150         {
3151           /* If the defining statement is in the basic block already
3152              we do not need to create a new expression for it, we
3153              only need to ensure its operands are expanded.  */
3154           expand_scalar_variables_stmt (def_stmt, gbb, scop,
3155                                         old_loop_father, ivstack);
3156           return op0;
3157           
3158         }
3159       else
3160         {
3161           if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3162             return op0;
3163           
3164           var0 = gimple_assign_rhs1 (def_stmt);
3165           subcode = gimple_assign_rhs_code (def_stmt);
3166           var1 = gimple_assign_rhs2 (def_stmt);
3167           
3168           return expand_scalar_variables_expr (type, var0, subcode, var1, 
3169                                                gbb, scop, old_loop_father, 
3170                                                ivstack);
3171         }
3172     }
3173
3174   gcc_unreachable ();
3175   return NULL;
3176 }
3177
3178 /* Replicates any uses of non-parameters and non-old-ivs variablesthat
3179    are defind outside GBB with code that is inserted in GBB.
3180    OLD_LOOP_FATHER is the original loop that contained STMT.  */
3181  
3182 static void
3183 expand_scalar_variables_stmt (gimple stmt, graphite_bb_p gbb, scop_p scop,
3184                               loop_p old_loop_father, loop_iv_stack ivstack)
3185 {
3186   ssa_op_iter iter;
3187   use_operand_p use_p;
3188   basic_block bb = GBB_BB (gbb);
3189
3190   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3191     {
3192       tree use = USE_FROM_PTR (use_p);
3193       tree type = TREE_TYPE (use);
3194       enum tree_code code  = TREE_CODE (use);
3195       tree use_expr = expand_scalar_variables_expr (type, use, code, NULL,
3196                                                     gbb, scop, old_loop_father, 
3197                                                     ivstack);
3198       if (use_expr != use)
3199         {
3200           gimple_stmt_iterator gsi = gsi_after_labels (bb);
3201           tree new_use =
3202             force_gimple_operand_gsi (&gsi, use_expr, true, NULL,
3203                                       true, GSI_NEW_STMT);
3204           SET_USE (use_p, new_use);
3205         }
3206     }
3207 }
3208
3209 /* Copies the definitions outside of GBB of variables that are not
3210    induction variables nor parameters. GBB must only contain
3211    "external" references to these types of variables.  OLD_LOOP_FATHER
3212    is the original loop that contained GBB.  */
3213
3214 static void 
3215 expand_scalar_variables (graphite_bb_p gbb, scop_p scop, 
3216                          loop_p old_loop_father, loop_iv_stack ivstack)
3217 {
3218   basic_block bb = GBB_BB (gbb);
3219   gimple_stmt_iterator gsi;
3220   
3221   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
3222     {
3223       gimple stmt = gsi_stmt (gsi);
3224       expand_scalar_variables_stmt (stmt, gbb, scop, old_loop_father, 
3225                                     ivstack); 
3226       gsi_next (&gsi);
3227     }
3228 }
3229
3230 /* Rename all the SSA_NAMEs from block GBB that appear in IVSTACK in
3231    terms of new induction variables.  OLD_LOOP_FATHER is the original
3232    loop that contained GBB.  */
3233
3234 static void 
3235 graphite_rename_ivs (graphite_bb_p gbb, scop_p scop, loop_p old_loop_father,
3236                      loop_iv_stack ivstack)
3237 {
3238   basic_block bb = GBB_BB (gbb);
3239   gimple_stmt_iterator gsi;
3240   
3241   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
3242     {
3243       gimple stmt = gsi_stmt (gsi);
3244
3245       if (gimple_get_lhs (stmt)
3246           && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
3247           && get_old_iv_from_ssa_name (scop, old_loop_father,
3248                                        gimple_get_lhs (stmt)))
3249         gsi_remove (&gsi, false);
3250       else
3251         {
3252           graphite_rename_ivs_stmt (stmt, gbb, scop, old_loop_father, ivstack); 
3253           gsi_next (&gsi);
3254         }
3255     }
3256 }
3257
3258 /* Move all the PHI nodes from block FROM to block TO.
3259    OLD_LOOP_FATHER is the original loop that contained FROM.  */
3260
3261 static void
3262 move_phi_nodes (scop_p scop, loop_p old_loop_father, basic_block from,
3263                 basic_block to)
3264 {
3265   gimple_stmt_iterator gsi;
3266
3267   for (gsi = gsi_start_phis (from); !gsi_end_p (gsi);)
3268     {
3269       gimple phi = gsi_stmt (gsi);
3270       tree op = gimple_phi_result (phi);
3271
3272       if (get_old_iv_from_ssa_name (scop, old_loop_father, op) == NULL)
3273         {
3274           gimple new_phi = make_phi_node (op, 0);
3275           add_phi_node_to_bb (new_phi, to);
3276         }
3277       remove_phi_node (&gsi, false);
3278     }
3279 }
3280
3281 /* Remove condition from BB.  */
3282
3283 static void
3284 remove_condition (basic_block bb)
3285 {
3286   gimple last = last_stmt (bb);
3287
3288   if (last && gimple_code (last) == GIMPLE_COND)
3289     {
3290       gimple_stmt_iterator gsi = gsi_last_bb (bb);
3291       gsi_remove (&gsi, true);
3292     }
3293 }
3294
3295 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set.  */
3296
3297 static edge
3298 get_true_edge_from_guard_bb (basic_block bb)
3299 {
3300   edge e;
3301   edge_iterator ei;
3302
3303   FOR_EACH_EDGE (e, ei, bb->succs)
3304     if (e->flags & EDGE_TRUE_VALUE) 
3305       return e;
3306
3307   gcc_unreachable ();
3308   return NULL;
3309 }
3310
3311 /* Translates a CLAST statement STMT to GCC representation.  NEXT_E is
3312    the edge where new generated code should be attached.  BB_EXIT is the last
3313    basic block that defines the scope of code generation.  CONTEXT_LOOP is the
3314    loop in which the generated code will be placed (might be NULL).  */
3315
3316 static edge
3317 translate_clast (scop_p scop, struct loop *context_loop,
3318                  struct clast_stmt *stmt, edge next_e, loop_iv_stack ivstack)
3319 {
3320   if (!stmt)
3321     return next_e;
3322
3323   if (CLAST_STMT_IS_A (stmt, stmt_root))
3324     return translate_clast (scop, context_loop, stmt->next, next_e, ivstack);
3325
3326   if (CLAST_STMT_IS_A (stmt, stmt_user))
3327     {
3328       CloogStatement *cs = ((struct clast_user_stmt *) stmt)->statement;
3329       graphite_bb_p gbb = (graphite_bb_p) cloog_statement_usr (cs);
3330       basic_block bb = gbb->bb;
3331       loop_p old_loop_father = bb->loop_father;
3332
3333       if (bb == ENTRY_BLOCK_PTR)
3334         return next_e;
3335
3336       remove_condition (bb);
3337       expand_scalar_variables (gbb, scop, old_loop_father, ivstack);
3338       remove_all_edges (bb, next_e);
3339       move_phi_nodes (scop, old_loop_father, bb, next_e->src);  
3340       redirect_edge_succ_nodup (next_e, bb);
3341
3342       if (context_loop)
3343         {
3344           remove_bb_from_loops (bb);
3345           add_bb_to_loop (bb, context_loop);
3346         }
3347
3348       set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src); 
3349       mark_virtual_ops_in_bb (bb);
3350       next_e = make_edge (bb,
3351                           context_loop ? context_loop->latch : EXIT_BLOCK_PTR,
3352                           EDGE_FALLTHRU);;
3353       graphite_rename_ivs (gbb, scop, old_loop_father, ivstack);
3354       return translate_clast (scop, context_loop, stmt->next, next_e, ivstack);
3355     }
3356
3357   if (CLAST_STMT_IS_A (stmt, stmt_for))
3358     {
3359       struct loop *loop
3360         = graphite_create_new_loop (scop, next_e, (struct clast_for *) stmt,
3361                                     ivstack, context_loop ? context_loop
3362                                     : get_loop (0));
3363       edge last_e = single_exit (loop);
3364         
3365       next_e = translate_clast (scop, loop, ((struct clast_for *) stmt)->body,
3366                                 single_pred_edge (loop->latch), ivstack);
3367       redirect_edge_succ_nodup (next_e, loop->latch);
3368         
3369       set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
3370       loop_iv_stack_pop (ivstack);
3371
3372       return translate_clast (scop, context_loop, stmt->next, last_e, ivstack);
3373     }
3374
3375   if (CLAST_STMT_IS_A (stmt, stmt_guard))
3376     {
3377       edge last_e = graphite_create_new_guard (scop, next_e,
3378                                                ((struct clast_guard *) stmt),
3379                                                ivstack);
3380       edge true_e = get_true_edge_from_guard_bb (next_e->dest);
3381       next_e = translate_clast (scop, context_loop, 
3382                                 ((struct clast_guard *) stmt)->then,
3383                                 true_e, ivstack);
3384       redirect_edge_succ_nodup (next_e, last_e->src);
3385       return translate_clast (scop, context_loop, stmt->next, last_e, ivstack);
3386     }
3387
3388   if (CLAST_STMT_IS_A (stmt, stmt_block))
3389     {
3390       next_e = translate_clast (scop, context_loop,
3391                                 ((struct clast_block *) stmt)->body,
3392                                 next_e, ivstack);
3393       return translate_clast (scop, context_loop, stmt->next, next_e, ivstack);
3394     }
3395
3396   gcc_unreachable ();
3397 }
3398
3399 /* Build cloog program for SCoP.  */
3400
3401 static void
3402 build_cloog_prog (scop_p scop)
3403 {
3404   int i;
3405   int max_nb_loops = scop_max_loop_depth (scop);
3406   graphite_bb_p gbb;
3407   CloogLoop *loop_list = NULL;
3408   CloogBlockList *block_list = NULL;
3409   CloogDomainList *scattering = NULL;
3410   CloogProgram *prog = SCOP_PROG (scop);
3411   int nbs = 2 * max_nb_loops + 1;
3412   int *scaldims = (int *) xmalloc (nbs * (sizeof (int)));
3413
3414   cloog_program_set_nb_scattdims (prog, nbs);
3415   initialize_cloog_names (scop);
3416
3417   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++)
3418     {
3419       /* Build new block.  */
3420       CloogMatrix *domain = GBB_DOMAIN (gbb);
3421       CloogStatement *stmt = cloog_statement_alloc (GBB_BB (gbb)->index);
3422       CloogBlock *block = cloog_block_alloc (stmt, 0, NULL,
3423                                              nb_loops_around_gb (gbb));
3424       cloog_statement_set_usr (stmt, gbb);
3425
3426       /* Add empty domain to all bbs, which do not yet have a domain, as they
3427          are not part of any loop.  */
3428       if (domain == NULL)
3429         {
3430           domain = cloog_matrix_alloc (0, scop_nb_params (scop) + 2);
3431           GBB_DOMAIN (gbb) = domain;
3432         }
3433
3434       /* Build loop list.  */
3435       {
3436         CloogLoop *new_loop_list = cloog_loop_malloc ();
3437         cloog_loop_set_next (new_loop_list, loop_list);
3438         cloog_loop_set_domain (new_loop_list,
3439                                cloog_domain_matrix2domain (domain));
3440         cloog_loop_set_block (new_loop_list, block);
3441         loop_list = new_loop_list;
3442       }
3443
3444       /* Build block list.  */
3445       {
3446         CloogBlockList *new_block_list = cloog_block_list_malloc ();
3447
3448         cloog_block_list_set_next (new_block_list, block_list);
3449         cloog_block_list_set_block (new_block_list, block);
3450         block_list = new_block_list;
3451       }
3452
3453       /* Build scattering list.  */
3454       {
3455         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
3456         CloogDomainList *new_scattering
3457           = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
3458         CloogMatrix *scat_mat = schedule_to_scattering (gbb, nbs);
3459
3460         cloog_set_next_domain (new_scattering, scattering);
3461         cloog_set_domain (new_scattering,
3462                           cloog_domain_matrix2domain (scat_mat));
3463         scattering = new_scattering;
3464         cloog_matrix_free (scat_mat);
3465       }
3466     }
3467
3468   cloog_program_set_loop (prog, loop_list);
3469   cloog_program_set_blocklist (prog, block_list);
3470
3471   for (i = 0; i < nbs; i++)
3472     scaldims[i] = 0 ;
3473
3474   cloog_program_set_scaldims (prog, scaldims);
3475
3476   /* Extract scalar dimensions to simplify the code generation problem.  */
3477   cloog_program_extract_scalars (prog, scattering);
3478
3479   /* Apply scattering.  */
3480   cloog_program_scatter (prog, scattering);
3481
3482   /* Iterators corresponding to scalar dimensions have to be extracted.  */
3483   cloog_names_scalarize (cloog_program_names (prog), nbs,
3484                          cloog_program_scaldims (prog));
3485   
3486   /* Free blocklist.  */
3487   {
3488     CloogBlockList *next = cloog_program_blocklist (prog);
3489         
3490     while (next)
3491       {
3492         CloogBlockList *toDelete = next;
3493         next = cloog_block_list_next (next);
3494         cloog_block_list_set_next (toDelete, NULL);
3495         cloog_block_list_set_block (toDelete, NULL);
3496         cloog_block_list_free (toDelete);
3497       }
3498     cloog_program_set_blocklist (prog, NULL);
3499   }
3500 }
3501
3502 /* Return the options that will be used in GLOOG.  */
3503
3504 static CloogOptions *
3505 set_cloog_options (void)
3506 {
3507   CloogOptions *options = cloog_options_malloc ();
3508
3509   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
3510      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
3511      we pass an incomplete program to cloog.  */
3512   options->language = LANGUAGE_C;
3513
3514   /* Enable complex equality spreading: removes dummy statements
3515      (assignments) in the generated code which repeats the
3516      substitution equations for statements.  This is useless for
3517      GLooG.  */
3518   options->esp = 1;
3519
3520   /* Enable C pretty-printing mode: normalizes the substitution
3521      equations for statements.  */
3522   options->cpp = 1;
3523
3524   /* Allow cloog to build strides with a stride width different to one.
3525      This example has stride = 4:
3526
3527      for (i = 0; i < 20; i += 4)
3528        A  */
3529   options->strides = 1;
3530
3531   /* Disable optimizations and make cloog generate source code closer to the
3532      input.  This is useful for debugging,  but later we want the optimized
3533      code.
3534
3535      XXX: We can not disable optimizations, as loop blocking is not working
3536      without them.  */
3537   if (0)
3538     {
3539       options->f = -1;
3540       options->l = INT_MAX;
3541     }
3542
3543   return options;
3544 }
3545
3546 /* Prints STMT to STDERR.  */
3547
3548 void
3549 debug_clast_stmt (struct clast_stmt *stmt)
3550 {
3551   CloogOptions *options = set_cloog_options ();
3552
3553   pprint (stderr, stmt, 0, options);
3554 }
3555
3556 /* Find the right transform for the SCOP, and return a Cloog AST
3557    representing the new form of the program.  */
3558
3559 static struct clast_stmt *
3560 find_transform (scop_p scop)
3561 {
3562   CloogProgram *prog;
3563   struct clast_stmt *stmt;
3564   CloogOptions *options = set_cloog_options ();
3565
3566   /* Connect new cloog prog generation to graphite.  */
3567   build_cloog_prog (scop);
3568
3569   if (dump_file && (dump_flags & TDF_DETAILS))
3570     {
3571       fprintf (dump_file, "Cloog Input [\n");
3572       cloog_program_print (dump_file, SCOP_PROG(scop));
3573       fprintf (dump_file, "]\n");
3574     }
3575
3576   prog = cloog_program_generate (SCOP_PROG (scop), options);
3577   stmt = cloog_clast_create (prog, options);
3578
3579   if (dump_file && (dump_flags & TDF_DETAILS))
3580     {
3581       fprintf (dump_file, "Cloog Output[\n");
3582       pprint (dump_file, stmt, 0, options);
3583       cloog_program_dump_cloog (dump_file, prog);
3584       fprintf (dump_file, "]\n");
3585     }
3586
3587   cloog_options_free (options);
3588   return stmt;
3589 }
3590
3591 /* Return a vector of all the virtual phi nodes in the current
3592    function.  */
3593  
3594 static VEC (gimple, heap) *
3595 collect_virtual_phis (void)     
3596 {
3597   gimple_stmt_iterator si;
3598   gimple_vec phis = VEC_alloc (gimple, heap, 3);
3599   basic_block bb;
3600
3601   FOR_EACH_BB (bb) 
3602     for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
3603       /* The phis we moved will have 0 arguments because the
3604          original edges were removed.  */
3605       if (gimple_phi_num_args (gsi_stmt (si)) == 0)
3606         VEC_safe_push (gimple, heap, phis, gsi_stmt (si));
3607
3608   /* Deallocate if we did not find any.  */
3609   if (VEC_length (gimple, phis) == 0)
3610     {
3611       VEC_free (gimple, heap, phis);
3612       phis = NULL;
3613     }
3614
3615   return phis;
3616 }
3617
3618 /* Find a virtual definition for variable VAR in BB.  */
3619
3620 static tree
3621 find_vdef_for_var_in_bb (basic_block bb, tree var)
3622 {
3623   gimple_stmt_iterator gsi;
3624   gimple phi;
3625   def_operand_p def_var;
3626   vuse_vec_p vv;
3627   ssa_op_iter op_iter;
3628
3629   for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
3630     FOR_EACH_SSA_VDEF_OPERAND (def_var, vv, gsi_stmt (gsi), op_iter)
3631       if (SSA_NAME_VAR (*def_var) == var)
3632         return *def_var;
3633
3634   for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
3635     FOR_EACH_SSA_DEF_OPERAND (def_var, gsi_stmt (gsi), op_iter, SSA_OP_DEF)
3636       if (SSA_NAME_VAR (*def_var) == var)
3637         return *def_var;
3638
3639   for (gsi = gsi_start_phis (bb); !gsi_end_p(gsi); gsi_next (&gsi))
3640     {
3641       phi = gsi_stmt (gsi);
3642       if (SSA_NAME_VAR (PHI_RESULT (phi)) == var)
3643         return PHI_RESULT (phi);
3644     }
3645
3646   return NULL;
3647 }
3648
3649 /* Recursive helper.  */
3650
3651 static tree
3652 find_vdef_for_var_1 (basic_block bb, struct pointer_set_t *visited, tree var)
3653 {
3654   tree result = NULL;
3655   edge_iterator ei;
3656   edge pred_edge;
3657
3658   if (pointer_set_contains (visited, bb))
3659     return NULL;
3660
3661   pointer_set_insert (visited, bb);
3662   result = find_vdef_for_var_in_bb (bb, var);
3663
3664   if (!result)
3665     FOR_EACH_EDGE (pred_edge, ei, bb->preds)
3666       if (!result)
3667         result = find_vdef_for_var_1 (pred_edge->src, visited, var);
3668
3669   return result;
3670 }
3671
3672 /* Finds a virtual definition for variable VAR.  */
3673
3674 static tree
3675 find_vdef_for_var (basic_block bb, tree var)
3676 {
3677   struct pointer_set_t *visited = pointer_set_create ();
3678   tree def = find_vdef_for_var_1 (bb, visited, var);
3679
3680   pointer_set_destroy (visited);
3681   return def;
3682 }
3683
3684 /* Update the virtual phis after loop bodies are moved to new
3685    loops.  */
3686
3687 static void
3688 patch_phis_for_virtual_defs (void)
3689 {
3690   int i;
3691   gimple phi;
3692   VEC (gimple, heap) *virtual_phis = collect_virtual_phis ();
3693   
3694   for (i = 0; VEC_iterate (gimple, virtual_phis, i, phi); i++)
3695     {
3696       basic_block bb = gimple_bb (phi);
3697       edge_iterator ei;
3698       edge pred_edge;
3699       gimple_stmt_iterator gsi;
3700       gimple new_phi;
3701       tree phi_result = PHI_RESULT (phi);
3702       tree var = SSA_NAME_VAR (phi_result);
3703
3704       new_phi = create_phi_node (phi_result, bb);
3705       SSA_NAME_DEF_STMT (phi_result) = new_phi;
3706
3707       FOR_EACH_EDGE (pred_edge, ei, bb->preds)
3708         {
3709           tree def = find_vdef_for_var (pred_edge->src, var);
3710
3711           if (def)
3712             add_phi_arg (new_phi, def, pred_edge);
3713           else
3714             add_phi_arg (new_phi, gimple_default_def (cfun, var), pred_edge);
3715         }
3716
3717       gsi = gsi_for_stmt (phi);
3718       remove_phi_node (&gsi, false);
3719     }
3720 }
3721
3722 /* Mark the original loops of SCOP for removal, replacing their header
3723    field with NULL.  */
3724
3725 static void
3726 mark_old_loops (scop_p scop)
3727 {
3728   int i;
3729   struct loop *loop;
3730
3731   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop); i++)
3732     {
3733       loop->header = NULL;
3734       loop->latch = NULL;
3735     }
3736 }
3737
3738 /* Scan the loops and remove the ones that have been marked for
3739    removal.  */
3740
3741 static void
3742 remove_dead_loops (void)
3743 {
3744   struct loop *loop, *ploop;
3745   loop_iterator li;
3746
3747   FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
3748     {
3749       /* Remove only those loops that we marked to be removed with
3750          mark_old_loops.  */
3751       if (loop->header)
3752         continue;
3753
3754       while (loop->inner)
3755         {
3756           ploop = loop->inner;
3757           flow_loop_tree_node_remove (ploop);
3758           flow_loop_tree_node_add (loop_outer (loop), ploop);
3759         }
3760
3761       /* Remove the loop and free its data.  */
3762       delete_loop (loop);
3763     }
3764 }
3765
3766 /* Returns true when it is possible to generate code for this STMT.
3767    For the moment we cannot generate code when Cloog decides to
3768    duplicate a statement, as we do not do a copy, but a move.
3769    USED_BASIC_BLOCKS records the blocks that have already been seen.
3770    We return false if we have to generate code twice for the same
3771    block.  */
3772
3773 static bool 
3774 can_generate_code_stmt (struct clast_stmt *stmt,
3775                         struct pointer_set_t *used_basic_blocks)
3776 {
3777   if (!stmt)
3778     return true;
3779
3780   if (CLAST_STMT_IS_A (stmt, stmt_root))
3781     return can_generate_code_stmt (stmt->next, used_basic_blocks);
3782
3783   if (CLAST_STMT_IS_A (stmt, stmt_user))
3784     {
3785       CloogStatement *cs = ((struct clast_user_stmt *) stmt)->statement;
3786       graphite_bb_p gbb = (graphite_bb_p) cloog_statement_usr (cs);
3787
3788       if (pointer_set_contains (used_basic_blocks, gbb))
3789         return false;
3790       pointer_set_insert (used_basic_blocks, gbb);
3791       return can_generate_code_stmt (stmt->next, used_basic_blocks);
3792     }
3793
3794   if (CLAST_STMT_IS_A (stmt, stmt_for))
3795     return can_generate_code_stmt (((struct clast_for *) stmt)->body,
3796                                    used_basic_blocks)
3797       && can_generate_code_stmt (stmt->next, used_basic_blocks);
3798
3799   if (CLAST_STMT_IS_A (stmt, stmt_guard))
3800     return can_generate_code_stmt (((struct clast_guard *) stmt)->then,
3801                                    used_basic_blocks);
3802
3803   if (CLAST_STMT_IS_A (stmt, stmt_block))
3804     return can_generate_code_stmt (((struct clast_block *) stmt)->body,
3805                                    used_basic_blocks)
3806       && can_generate_code_stmt (stmt->next, used_basic_blocks);
3807
3808   return false;
3809 }
3810
3811 /* Returns true when it is possible to generate code for this STMT.  */
3812
3813 static bool 
3814 can_generate_code (struct clast_stmt *stmt)
3815 {
3816   bool result;
3817   struct pointer_set_t *used_basic_blocks = pointer_set_create ();
3818
3819   result = can_generate_code_stmt (stmt, used_basic_blocks);
3820   pointer_set_destroy (used_basic_blocks);
3821   return result;
3822 }
3823
3824 /* Skip any definition that is a phi node with a single phi def.  */
3825
3826 static tree 
3827 skip_phi_defs (tree ssa_name)
3828 {
3829   tree result = ssa_name;
3830   gimple def_stmt = SSA_NAME_DEF_STMT (ssa_name);
3831
3832   if (gimple_code (def_stmt) == GIMPLE_PHI 
3833       && gimple_phi_num_args (def_stmt) == 1)
3834     result = skip_phi_defs (gimple_phi_arg(def_stmt,0)->def);
3835
3836   return result;
3837 }
3838
3839 /* Returns a VEC containing the phi-arg defs coming from SCOP_EXIT in
3840    the destination block of SCOP_EXIT.  */
3841
3842 static VEC (tree, heap) *
3843 collect_scop_exit_phi_args (edge scop_exit)
3844 {
3845   VEC (tree, heap) *phi_args = VEC_alloc (tree, heap, 1);
3846   gimple_stmt_iterator gsi;
3847
3848   for (gsi = gsi_start_phis (scop_exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
3849     {
3850       gimple phi = gsi_stmt (gsi);
3851       tree phi_arg = skip_phi_defs(PHI_ARG_DEF_FROM_EDGE (phi, scop_exit));
3852
3853       VEC_safe_push (tree, heap, phi_args, phi_arg);
3854     }
3855
3856   return phi_args;
3857 }
3858
3859 /* Patches (adds) PHI_ARGS to the phi nodes in SCOP_EXIT destination.  */
3860
3861 static void
3862 patch_scop_exit_phi_args (edge scop_exit,
3863                           VEC (tree, heap) *phi_args)
3864 {
3865   int i = 0;
3866   gimple_stmt_iterator gsi;
3867
3868   for (gsi = gsi_start_phis (scop_exit->dest); !gsi_end_p (gsi);
3869        gsi_next (&gsi), i++)
3870     {
3871       tree def = VEC_index (tree, phi_args, i);
3872       gimple phi = gsi_stmt (gsi);
3873
3874       gcc_assert (PHI_ARG_DEF_FROM_EDGE (phi, scop_exit) == NULL);
3875
3876       add_phi_arg (phi, def, scop_exit);
3877     }
3878 }
3879
3880 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
3881    the given SCOP.  */
3882
3883 static void
3884 gloog (scop_p scop, struct clast_stmt *stmt)
3885 {
3886   edge new_scop_exit_edge = NULL;
3887   basic_block scop_exit = SCOP_EXIT (scop);
3888   VEC (tree, heap)* phi_args = 
3889     collect_scop_exit_phi_args (SESE_EXIT (SCOP_REGION (scop)));
3890   VEC (name_tree, heap) *ivstack = VEC_alloc (name_tree, heap, 10);
3891   edge construction_edge = SESE_ENTRY (SCOP_REGION (scop));
3892   basic_block old_scop_exit_idom = get_immediate_dominator (CDI_DOMINATORS,
3893                                                             scop_exit);
3894
3895   if (!can_generate_code (stmt))
3896     {
3897       cloog_clast_free (stmt);
3898       return;
3899     }
3900
3901   new_scop_exit_edge = translate_clast (scop, 
3902                                         construction_edge->src->loop_father,
3903                                         stmt, construction_edge, &ivstack);
3904   redirect_edge_succ (new_scop_exit_edge, scop_exit);
3905   if (!old_scop_exit_idom
3906       || !dominated_by_p (CDI_DOMINATORS, SCOP_ENTRY (scop),
3907                           old_scop_exit_idom)
3908       || SCOP_ENTRY (scop) == old_scop_exit_idom)
3909     set_immediate_dominator (CDI_DOMINATORS,
3910                              new_scop_exit_edge->dest,
3911                              new_scop_exit_edge->src);
3912
3913   cloog_clast_free (stmt);
3914
3915   if (new_scop_exit_edge->dest == EXIT_BLOCK_PTR)
3916     new_scop_exit_edge->flags = 0;
3917  
3918   find_unreachable_blocks ();
3919   delete_unreachable_blocks ();
3920   patch_phis_for_virtual_defs ();
3921   patch_scop_exit_phi_args (new_scop_exit_edge, phi_args);
3922   mark_old_loops (scop);
3923   remove_dead_loops ();
3924   rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); 
3925
3926 #ifdef ENABLE_CHECKING
3927   verify_loop_structure ();
3928   verify_dominators (CDI_DOMINATORS);
3929   verify_ssa (false);
3930 #endif
3931 }
3932
3933 /* Returns the number of data references in SCOP.  */
3934
3935 static int
3936 nb_data_refs_in_scop (scop_p scop)
3937 {
3938   int i;
3939   graphite_bb_p gbb;
3940   int res = 0;
3941
3942   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++)
3943     res += VEC_length (data_reference_p, GBB_DATA_REFS (gbb));
3944
3945   return res;
3946 }
3947
3948 /* Check if a graphite bb can be ignored in graphite.  We ignore all
3949    bbs, that only contain code, that will be eliminated later.
3950
3951    TODO: - Move PHI nodes and scalar variables out of these bbs, that only
3952            remain conditions and induction variables.  */
3953
3954 static bool
3955 gbb_can_be_ignored (graphite_bb_p gb)
3956 {
3957   gimple_stmt_iterator gsi;
3958   scop_p scop = GBB_SCOP (gb);
3959   loop_p loop = GBB_BB (gb)->loop_father;
3960
3961   if (VEC_length (data_reference_p, GBB_DATA_REFS(gb)))
3962     return false;
3963
3964   /* Check statements.  */
3965   for (gsi = gsi_start_bb (GBB_BB (gb)); !gsi_end_p (gsi); gsi_next (&gsi))
3966     {
3967       gimple stmt = gsi_stmt (gsi);
3968       switch (gimple_code (stmt))
3969         {
3970           /* Control flow expressions can be ignored, as they are
3971              represented in the iteration domains and will be
3972              regenerated by graphite.  */
3973           case GIMPLE_COND:
3974           case GIMPLE_GOTO:
3975           case GIMPLE_SWITCH:
3976             break;
3977
3978           /* Scalar variables can be ignored, if we can regenerate
3979              them later using their scalar evolution function.
3980              XXX: Just a heuristic, that needs further investigation.  */
3981           case GIMPLE_ASSIGN:
3982             {
3983               tree var = gimple_assign_lhs (stmt);
3984               var = analyze_scalar_evolution (loop, var);
3985               var = instantiate_scev (block_before_scop (scop), loop, var);
3986
3987               if (TREE_CODE (var) == SCEV_NOT_KNOWN)
3988                 return false;
3989
3990               break;
3991             }
3992           /* Otherwise not ignoreable.  */
3993           default:
3994             return false;
3995         }
3996     }
3997
3998   return true;
3999 }
4000
4001 /* Remove all ignoreable gbbs from SCOP.  */
4002
4003 static void
4004 scop_remove_ignoreable_gbbs (scop_p scop)
4005 {
4006   graphite_bb_p gb;
4007   int i;
4008
4009   int max_schedule = scop_max_loop_depth (scop) + 1;
4010   lambda_vector last_schedule = lambda_vector_new (max_schedule);
4011   lambda_vector_clear (last_schedule, max_schedule);
4012
4013   /* Update schedules.  */
4014   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
4015     {
4016       int nb_loops = gbb_nb_loops (gb);
4017
4018       if (GBB_STATIC_SCHEDULE (gb) [nb_loops] == 0)
4019         last_schedule [nb_loops] = 0;
4020
4021       if (gbb_can_be_ignored (gb))
4022         {
4023           /* Mark gbb for remove.  */
4024           bitmap_clear_bit (SCOP_BBS_B (scop), gb->bb->index);
4025           GBB_SCOP (gb) = NULL;
4026           last_schedule [nb_loops]--;
4027         }
4028       else
4029         lambda_vector_add (GBB_STATIC_SCHEDULE (gb), last_schedule,
4030                            GBB_STATIC_SCHEDULE (gb), nb_loops + 1);
4031     }
4032
4033   /* Remove gbbs.  */
4034   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
4035     if (GBB_SCOP (gb) == NULL)
4036       {
4037         VEC_unordered_remove (graphite_bb_p, SCOP_BBS (scop), i);
4038         free_graphite_bb (gb);
4039         /* XXX: Hackish? But working.  */
4040         i--;
4041       }  
4042
4043   graphite_sort_gbbs (scop);
4044 }
4045
4046 /* Move the loop at index LOOP and insert it before index NEW_LOOP_POS.
4047    This transformartion is only valid, if the loop nest between i and k is
4048    perfectly nested. Therefore we do not need to change the static schedule.
4049
4050    Example:
4051
4052    for (i = 0; i < 50; i++)
4053      for (j ...)
4054        for (k = 5; k < 100; k++)
4055          A
4056
4057    To move k before i use:
4058
4059    graphite_trans_bb_move_loop (A, 2, 0)
4060
4061    for (k = 5; k < 100; k++)
4062      for (i = 0; i < 50; i++)
4063        for (j ...)
4064          A
4065
4066    And to move k back:
4067
4068    graphite_trans_bb_move_loop (A, 0, 2)
4069
4070    This function does not check the validity of interchanging loops.
4071    This should be checked before calling this function.  */
4072
4073 static void
4074 graphite_trans_bb_move_loop (graphite_bb_p gb, int loop,
4075                              int new_loop_pos)
4076 {
4077   CloogMatrix *domain = GBB_DOMAIN (gb);
4078   int row, j;
4079   loop_p tmp_loop_p;
4080
4081   gcc_assert (loop < gbb_nb_loops (gb)
4082               && new_loop_pos < gbb_nb_loops (gb));
4083
4084   /* Update LOOPS vector.  */
4085   tmp_loop_p = VEC_index (loop_p, GBB_LOOPS (gb), loop);
4086   VEC_ordered_remove (loop_p, GBB_LOOPS (gb), loop);
4087   VEC_safe_insert (loop_p, heap, GBB_LOOPS (gb), new_loop_pos, tmp_loop_p);
4088
4089   /* Move the domain columns.  */
4090   if (loop < new_loop_pos)
4091     for (row = 0; row < domain->NbRows; row++)
4092       {
4093         Value tmp;
4094         value_init (tmp);
4095         value_assign (tmp, domain->p[row][loop + 1]);
4096    
4097         for (j = loop ; j < new_loop_pos - 1; j++)
4098           value_assign (domain->p[row][j + 1], domain->p[row][j + 2]);
4099
4100         value_assign (domain->p[row][new_loop_pos], tmp);
4101         value_clear (tmp);
4102       }
4103   else
4104     for (row = 0; row < domain->NbRows; row++)
4105       {
4106         Value tmp;
4107         value_init (tmp);
4108         value_assign (tmp, domain->p[row][loop + 1]);
4109
4110         for (j = loop ; j > new_loop_pos; j--)
4111           value_assign (domain->p[row][j + 1], domain->p[row][j]);
4112      
4113         value_assign (domain->p[row][new_loop_pos + 1], tmp);
4114         value_clear (tmp);
4115       }
4116 }
4117
4118 /* Get the index of the column representing constants in the DOMAIN
4119    matrix.  */
4120
4121 static int
4122 const_column_index (CloogMatrix *domain)
4123 {
4124   return domain->NbColumns - 1;
4125 }
4126
4127
4128 /* Get the first index that is positive or negative, determined
4129    following the value of POSITIVE, in matrix DOMAIN in COLUMN.  */
4130
4131 static int
4132 get_first_matching_sign_row_index (CloogMatrix *domain, int column,
4133                                    bool positive)
4134 {
4135   int row;
4136
4137   for (row = 0; row < domain->NbRows; row++)
4138     {
4139       int val = value_get_si (domain->p[row][column]);
4140
4141       if (val > 0 && positive)
4142         return row;
4143
4144       else if (val < 0 && !positive)
4145         return row;
4146     }
4147
4148   gcc_unreachable ();
4149 }
4150
4151 /* Get the lower bound of COLUMN in matrix DOMAIN.  */
4152
4153 static int
4154 get_lower_bound_row (CloogMatrix *domain, int column)
4155 {
4156   return get_first_matching_sign_row_index (domain, column, true);
4157 }
4158
4159 /* Get the upper bound of COLUMN in matrix DOMAIN.  */
4160
4161 static int
4162 get_upper_bound_row (CloogMatrix *domain, int column)
4163 {
4164   return get_first_matching_sign_row_index (domain, column, false);
4165 }
4166
4167 /* Get the lower bound of LOOP.  */
4168
4169 static void
4170 get_lower_bound (CloogMatrix *domain, int loop, Value lower_bound_result)
4171 {
4172   int lower_bound_row = get_lower_bound_row (domain, loop);
4173   value_init (lower_bound_result);
4174   value_assign (lower_bound_result,
4175                 domain->p[lower_bound_row][const_column_index(domain)]);
4176 }
4177
4178 /* Get the upper bound of LOOP.  */
4179
4180 static void
4181 get_upper_bound (CloogMatrix *domain, int loop, Value upper_bound_result)
4182 {
4183   int upper_bound_row = get_upper_bound_row (domain, loop);
4184   value_init (upper_bound_result);
4185   value_assign (upper_bound_result,
4186                 domain->p[upper_bound_row][const_column_index(domain)]);
4187 }
4188
4189 /* Strip mines the loop of BB at the position LOOP_DEPTH with STRIDE.
4190    Always valid, but not always a performance improvement.  */
4191   
4192 static void
4193 graphite_trans_bb_strip_mine (graphite_bb_p gb, int loop_depth, int stride)
4194 {
4195   int row, col;
4196
4197   CloogMatrix *domain = GBB_DOMAIN (gb);
4198   CloogMatrix *new_domain = cloog_matrix_alloc (domain->NbRows + 3,
4199                                                 domain->NbColumns + 1);   
4200
4201   int col_loop_old = loop_depth + 2; 
4202   int col_loop_strip = col_loop_old - 1;
4203
4204   Value old_lower_bound;
4205   Value old_upper_bound;
4206
4207
4208   gcc_assert (loop_depth <= gbb_nb_loops (gb) - 1);
4209
4210   VEC_safe_insert (loop_p, heap, GBB_LOOPS (gb), loop_depth, NULL);
4211
4212   GBB_DOMAIN (gb) = new_domain;
4213
4214   /*
4215    nrows = 4, ncols = 4
4216   eq    i    j    c
4217    1    1    0    0 
4218    1   -1    0   99 
4219    1    0    1    0 
4220    1    0   -1   99 
4221   */
4222  
4223   /* Move domain.  */
4224   for (row = 0; row < domain->NbRows; row++)
4225     for (col = 0; col < domain->NbColumns; col++)
4226       if (col <= loop_depth)
4227         {
4228           value_assign (new_domain->p[row][col], domain->p[row][col]);
4229         }
4230       else
4231         {
4232           value_assign (new_domain->p[row][col + 1], domain->p[row][col]);
4233         }
4234
4235
4236   /*
4237     nrows = 6, ncols = 5
4238            outer inner
4239    eq   i   jj    j    c
4240    1    1    0    0    0 
4241    1   -1    0    0   99 
4242    1    0    0    1    0 
4243    1    0    0   -1   99 
4244    0    0    0    0    0 
4245    0    0    0    0    0 
4246    0    0    0    0    0 
4247    */
4248
4249   row = domain->NbRows;
4250
4251   /* Add outer loop.  */
4252
4253   get_lower_bound (new_domain, col_loop_old, old_lower_bound);
4254   get_upper_bound (new_domain, col_loop_old, old_upper_bound);
4255
4256   /* Set Lower Bound */
4257   value_set_si (new_domain->p[row][0], 1);
4258   value_set_si (new_domain->p[row][col_loop_strip], 1);
4259   value_assign (new_domain->p[row][const_column_index (new_domain)],
4260                 old_lower_bound);
4261   row++;
4262
4263
4264   /*
4265     6 5
4266    eq   i   jj    j    c
4267    1    1    0    0    0 
4268    1   -1    0    0   99 
4269    1    0    0    1    0  - 
4270    1    0    0   -1   99   | copy old lower bound
4271    1    0    1    0    0 <-
4272    0    0    0    0    0
4273    0    0    0    0    0
4274    */
4275
4276   {
4277     Value new_upper_bound;
4278     Value strip_size_value;
4279
4280     value_init (new_upper_bound);
4281
4282     value_init (strip_size_value);
4283     value_set_si (strip_size_value, (int) stride);
4284
4285   
4286     value_pdivision(new_upper_bound,old_upper_bound,strip_size_value);
4287     value_add_int (new_upper_bound, new_upper_bound, 1);
4288
4289     /* Set Upper Bound */
4290     value_set_si (new_domain->p[row][0], 1);
4291     value_set_si (new_domain->p[row][col_loop_strip], -1);
4292     value_assign (new_domain->p[row][const_column_index (new_domain)],
4293                   new_upper_bound);
4294     row++;
4295   }
4296   /*
4297     6 5
4298    eq   i   jj    j    c
4299    1    1    0    0    0 
4300    1   -1    0    0   99 
4301    1    0    0    1    0  
4302    1    0    0   -1   99  
4303    1    0    1    0    0 
4304    1    0   -1    0   25  (divide old upper bound with stride) 
4305    0    0    0    0    0
4306   */
4307
4308   {
4309     row = get_lower_bound_row (new_domain, col_loop_old);
4310     /* Add local variable to keep linear representation.  */
4311     value_set_si (new_domain->p[row][0], 1);
4312     value_set_si (new_domain->p[row][const_column_index (new_domain)],0);
4313     value_set_si (new_domain->p[row][col_loop_old], 1);
4314     value_set_si (new_domain->p[row][col_loop_strip], -1*((int)stride));
4315   }
4316
4317   /*
4318     6 5
4319    eq   i   jj    j    c
4320    1    1    0    0    0 
4321    1   -1    0    0   99 
4322    1    0    -1   1    0  
4323    1    0    0   -1   99  
4324    1    0    1    0    0 
4325    1    0   -1    0   25  (divide old upper bound with stride) 
4326    0    0    0    0    0
4327   */
4328
4329   {
4330     row = new_domain->NbRows-1;
4331     
4332     value_set_si (new_domain->p[row][0], 1);
4333     value_set_si (new_domain->p[row][col_loop_old], -1);
4334     value_set_si (new_domain->p[row][col_loop_strip], stride);
4335     value_set_si (new_domain->p[row][const_column_index (new_domain)],
4336                   stride-1);
4337   }
4338
4339   /*
4340     6 5
4341    eq   i   jj    j    c
4342    1    1    0    0    0     i >= 0
4343    1   -1    0    0   99    99 >= i
4344    1    0    -4   1    0     j >= 4*jj
4345    1    0    0   -1   99    99 >= j
4346    1    0    1    0    0    jj >= 0
4347    1    0   -1    0   25    25 >= jj
4348    0    0    4    -1   3  jj+3 >= j
4349   */
4350
4351   cloog_matrix_free (domain);
4352
4353   /* Update static schedule.  */
4354   {
4355     int i;
4356     int nb_loops = gbb_nb_loops (gb);
4357     lambda_vector new_schedule = lambda_vector_new (nb_loops + 1);
4358
4359     for (i = 0; i <= loop_depth; i++)
4360       new_schedule[i] = GBB_STATIC_SCHEDULE (gb)[i];  
4361
4362     for (i = loop_depth + 1; i <= nb_loops - 2; i++)
4363       new_schedule[i + 2] = GBB_STATIC_SCHEDULE (gb)[i];  
4364
4365     GBB_STATIC_SCHEDULE (gb) = new_schedule;
4366   }
4367 }
4368
4369 /* Returns true when the strip mining of LOOP_INDEX by STRIDE is
4370    profitable or undecidable.  GB is the statement around which the
4371    loops will be strip mined.  */
4372
4373 static bool
4374 strip_mine_profitable_p (graphite_bb_p gb, int stride,
4375                          int loop_index)
4376 {
4377   bool res = true;
4378   edge exit = NULL;
4379   tree niter;
4380   loop_p loop;
4381   long niter_val;
4382
4383   loop = VEC_index (loop_p, GBB_LOOPS (gb), loop_index);
4384   exit = single_exit (loop);
4385
4386   niter = find_loop_niter (loop, &exit);
4387   if (niter == chrec_dont_know 
4388       || TREE_CODE (niter) != INTEGER_CST)
4389     return true;
4390   
4391   niter_val = int_cst_value (niter);
4392
4393   if (niter_val < stride)
4394     {
4395       res = false;
4396       if (dump_file && (dump_flags & TDF_DETAILS))
4397         {
4398           fprintf (dump_file, "\nStrip Mining is not profitable for loop %d:",
4399                    loop_index);
4400           fprintf (dump_file, "number of iterations is too low.\n");
4401         }
4402     }
4403   
4404   return res;
4405 }
4406  
4407 /* Determines when the interchange of LOOP_A and LOOP_B belonging to
4408    SCOP is legal.  */
4409
4410 static bool
4411 is_interchange_valid (scop_p scop, int loop_a, int loop_b)
4412 {
4413   bool res;
4414   VEC (ddr_p, heap) *dependence_relations;
4415   VEC (data_reference_p, heap) *datarefs;
4416
4417   struct loop *nest = VEC_index (loop_p, SCOP_LOOP_NEST (scop), loop_a);
4418   int depth = perfect_loop_nest_depth (nest);
4419   lambda_trans_matrix trans;
4420
4421   gcc_assert (loop_a < loop_b);
4422
4423   dependence_relations = VEC_alloc (ddr_p, heap, 10 * 10);
4424   datarefs = VEC_alloc (data_reference_p, heap, 10);
4425
4426   if (!compute_data_dependences_for_loop (nest, true, &datarefs,
4427                                           &dependence_relations))
4428     return false;
4429
4430   if (dump_file && (dump_flags & TDF_DETAILS))
4431     dump_ddrs (dump_file, dependence_relations);
4432
4433   trans = lambda_trans_matrix_new (depth, depth);
4434   lambda_matrix_id (LTM_MATRIX (trans), depth);
4435
4436   lambda_matrix_row_exchange (LTM_MATRIX (trans), 0, loop_b - loop_a);
4437
4438   if (!lambda_transform_legal_p (trans, depth, dependence_relations))
4439     {
4440       lambda_matrix_row_exchange (LTM_MATRIX (trans), 0, loop_b - loop_a);
4441       res = false;
4442     }
4443   else
4444     res = true;
4445
4446   free_dependence_relations (dependence_relations);
4447   free_data_refs (datarefs);
4448   return res;
4449 }
4450
4451 /* Loop block the LOOPS innermost loops of GB with stride size STRIDE. 
4452
4453    Example
4454
4455    for (i = 0; i <= 50; i++=4) 
4456      for (k = 0; k <= 100; k++=4) 
4457        for (l = 0; l <= 200; l++=4) 
4458          A
4459
4460    To strip mine the two inner most loops with stride = 4 call:
4461
4462    graphite_trans_bb_block (A, 4, 2) 
4463
4464    for (i = 0; i <= 50; i++) 
4465      for (kk = 0; kk <= 100; kk+=4) 
4466        for (ll = 0; ll <= 200; ll+=4) 
4467          for (k = kk; k <= min (100, kk + 3); k++) 
4468            for (l = ll; l <= min (200, ll + 3); l++) 
4469              A
4470 */
4471
4472 static bool
4473 graphite_trans_bb_block (graphite_bb_p gb, int stride, int loops)
4474 {
4475   int i, j;
4476   int nb_loops = gbb_nb_loops (gb);
4477   int start = nb_loops - loops;
4478   scop_p scop = GBB_SCOP (gb);
4479
4480   gcc_assert (scop_contains_loop (scop, gbb_loop (gb)));
4481
4482   for (i = start ; i < nb_loops; i++)
4483     for (j = i + 1; j < nb_loops; j++)
4484       if (!is_interchange_valid (scop, i, j))
4485         {
4486           if (dump_file && (dump_flags & TDF_DETAILS))
4487             fprintf (dump_file,
4488                      "\nInterchange not valid for loops %d and %d:\n", i, j);
4489           return false;
4490         }
4491       else if (dump_file && (dump_flags & TDF_DETAILS))
4492         fprintf (dump_file,
4493                  "\nInterchange valid for loops %d and %d:\n", i, j);
4494
4495   /* Check if strip mining is profitable for every loop.  */
4496   for (i = 0; i < nb_loops - start; i++)
4497     if (!strip_mine_profitable_p (gb, stride, start + i))
4498       return false;
4499
4500   /* Strip mine loops.  */
4501   for (i = 0; i < nb_loops - start; i++)
4502     graphite_trans_bb_strip_mine (gb, start + 2 * i, stride);
4503
4504   /* Interchange loops.  */
4505   for (i = 1; i < nb_loops - start; i++)
4506     graphite_trans_bb_move_loop (gb, start + 2 * i, start + i);
4507
4508   return true;
4509 }
4510
4511 /* Loop block LOOPS innermost loops of a loop nest.  BBS represent the
4512    basic blocks that belong to the loop nest to be blocked.  */
4513
4514 static bool
4515 graphite_trans_loop_block (VEC (graphite_bb_p, heap) *bbs, int loops)
4516 {
4517   graphite_bb_p gb;
4518   int i;
4519   bool transform_done = false;
4520
4521   /* TODO: - Calculate the stride size automatically.  */
4522   int stride_size = 64;
4523
4524   for (i = 0; VEC_iterate (graphite_bb_p, bbs, i, gb); i++)
4525     transform_done |= graphite_trans_bb_block (gb, stride_size, loops);
4526
4527   return transform_done;
4528 }
4529
4530 /* Loop block all basic blocks of SCOP.  */
4531
4532 static bool
4533 graphite_trans_scop_block (scop_p scop)
4534 {
4535   graphite_bb_p gb;
4536   int i, j;
4537   int last_nb_loops;
4538   int nb_loops;
4539   bool perfect = true;
4540   bool transform_done = false;
4541
4542   VEC (graphite_bb_p, heap) *bbs = VEC_alloc (graphite_bb_p, heap, 3);
4543   int max_schedule = scop_max_loop_depth (scop) + 1;
4544   lambda_vector last_schedule = lambda_vector_new (max_schedule);
4545
4546   if (VEC_length (graphite_bb_p, SCOP_BBS (scop)) == 0)
4547     return transform_done;
4548
4549   /* Get the data of the first bb.  */
4550   gb = VEC_index (graphite_bb_p, SCOP_BBS (scop), 0); 
4551   last_nb_loops = gbb_nb_loops (gb);
4552   lambda_vector_copy (GBB_STATIC_SCHEDULE (gb), last_schedule,
4553                       last_nb_loops + 1);
4554   VEC_safe_push (graphite_bb_p, heap, bbs, gb);
4555   
4556   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
4557     {
4558       /* We did the first bb before.  */
4559       if (i == 0)
4560         continue;
4561
4562       nb_loops = gbb_nb_loops (gb);
4563
4564       /* If the number of loops is unchanged and only the last element of the
4565          schedule changes, we stay in the loop nest.  */
4566       if (nb_loops == last_nb_loops 
4567           && (last_schedule [nb_loops + 1]
4568               != GBB_STATIC_SCHEDULE (gb)[nb_loops + 1]))
4569         {
4570           VEC_safe_push (graphite_bb_p, heap, bbs, gb);
4571           continue;
4572         }
4573
4574       /* Otherwise, we left the innermost loop. So check, if the last bb was in
4575          a perfect loop nest and how many loops are contained in this perfect
4576          loop nest. 
4577          
4578          Count the number of zeros from the end of the schedule. They are the
4579          number of surrounding loops.
4580
4581          Example:
4582          last_bb  2 3 2 0 0 0 0 3
4583          bb       2 4 0
4584          <------  j = 4
4585         
4586          last_bb  2 3 2 0 0 0 0 3
4587          bb       2 3 2 0 1
4588          <--  j = 2
4589
4590          If there is no zero, there were other bbs in outer loops and the loop
4591          nest is not perfect.  */
4592       for (j = last_nb_loops - 1; j >= 0; j--)
4593         {
4594           if (last_schedule [j] != 0
4595               || (j <= nb_loops && GBB_STATIC_SCHEDULE (gb)[j] == 1))
4596             {
4597               j--;
4598               break;
4599             }
4600         }
4601       
4602       j++;
4603
4604       /* Found perfect loop nest.  */
4605       if (perfect && last_nb_loops - j > 0)
4606         transform_done |= graphite_trans_loop_block (bbs, last_nb_loops - j);
4607  
4608       /* Check if we start with a new loop.
4609
4610          Example:
4611   
4612          last_bb  2 3 2 0 0 0 0 3
4613          bb       2 3 2 0 0 1 0
4614         
4615          Here we start with the loop "2 3 2 0 0 1" 
4616
4617          last_bb  2 3 2 0 0 0 0 3
4618          bb       2 3 2 0 0 1 
4619
4620          But here not, so the loop nest can never be perfect.  */
4621
4622       perfect = (GBB_STATIC_SCHEDULE (gb)[nb_loops] == 0);
4623
4624       /* Update the last_bb infos.  We do not do that for the bbs in the same
4625          loop, as the data we use is not changed.  */
4626       last_nb_loops = nb_loops;
4627       lambda_vector_copy (GBB_STATIC_SCHEDULE (gb), last_schedule,
4628                           nb_loops + 1);
4629       VEC_truncate (graphite_bb_p, bbs, 0);
4630       VEC_safe_push (graphite_bb_p, heap, bbs, gb);
4631     }
4632
4633   /* Check if the last loop nest was perfect.  It is the same check as above,
4634      but the comparison with the next bb is missing.  */
4635   for (j = last_nb_loops - 1; j >= 0; j--)
4636     if (last_schedule [j] != 0)
4637       {
4638         j--;
4639         break;
4640       }
4641
4642   j++;
4643
4644   /* Found perfect loop nest.  */
4645   if (last_nb_loops - j > 0)
4646     transform_done |= graphite_trans_loop_block (bbs, last_nb_loops - j);
4647   VEC_free (graphite_bb_p, heap, bbs);
4648
4649   if (dump_file && (dump_flags & TDF_DETAILS))
4650     fprintf (dump_file, "\nLoop blocked.\n");
4651
4652   return transform_done;
4653 }
4654
4655 /* Apply graphite transformations to all the basic blocks of SCOP.  */
4656
4657 static bool
4658 graphite_apply_transformations (scop_p scop)
4659 {
4660   bool transform_done = false;
4661
4662   /* Sort the list of bbs.  Keep them always sorted.  */
4663   graphite_sort_gbbs (scop);
4664   scop_remove_ignoreable_gbbs (scop);
4665
4666   if (flag_loop_block)
4667     transform_done = graphite_trans_scop_block (scop);
4668
4669 #if 0 && ENABLE_CHECKING
4670   /* When the compiler is configured with ENABLE_CHECKING, always
4671      generate code, even if we did not apply any transformation.  This
4672      provides better code coverage of the backend code generator.
4673
4674      This also allows to check the performance for an identity
4675      transform: GIMPLE -> GRAPHITE -> GIMPLE; and the output of CLooG
4676      is never an identity: if CLooG optimizations are not disabled,
4677      the CLooG output is always optimized in control flow.  */
4678   transform_done = true;
4679 #endif
4680
4681   return transform_done;
4682 }
4683
4684 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop. 
4685
4686    Example:
4687
4688    for (i      |
4689      {         |
4690        for (j  |  SCoP 1
4691        for (k  |
4692      }         |
4693
4694    * SCoP frontier, as this line is not surrounded by any loop. *
4695
4696    for (l      |  SCoP 2
4697
4698    This is necessary as scalar evolution and parameter detection need a
4699    outermost loop to initialize parameters correctly.  
4700   
4701    TODO: FIX scalar evolution and parameter detection to allow mor flexible
4702          SCoP frontiers.  */
4703
4704 static void
4705 limit_scops (void)
4706 {
4707   VEC (scop_p, heap) *new_scops = VEC_alloc (scop_p, heap, 3);
4708   int i;
4709   scop_p scop;
4710
4711   for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
4712     {
4713       int j;
4714       loop_p loop;
4715       build_scop_bbs (scop);
4716       build_scop_loop_nests (scop);
4717
4718       for (j = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), j, loop); j++) 
4719         if (!loop_in_scop_p (loop_outer (loop), scop))
4720           {
4721             scop_p n_scop = new_scop (loop_preheader_edge (loop));
4722             end_scop (n_scop, single_exit (loop), false);
4723             VEC_safe_push (scop_p, heap, new_scops, n_scop);
4724           }
4725     }
4726
4727   free_scops (current_scops);
4728   current_scops = new_scops;
4729 }
4730
4731 /* Perform a set of linear transforms on the loops of the current
4732    function.  */
4733
4734 void
4735 graphite_transform_loops (void)
4736 {
4737   int i;
4738   scop_p scop;
4739
4740   if (number_of_loops () <= 1)
4741     return;
4742
4743   current_scops = VEC_alloc (scop_p, heap, 3);
4744
4745   calculate_dominance_info (CDI_DOMINATORS);
4746   calculate_dominance_info (CDI_POST_DOMINATORS);
4747
4748   if (dump_file && (dump_flags & TDF_DETAILS))
4749     fprintf (dump_file, "Graphite loop transformations \n");
4750
4751   cloog_initialize ();
4752   build_scops ();
4753   limit_scops ();
4754
4755   if (dump_file && (dump_flags & TDF_DETAILS))
4756     fprintf (dump_file, "\nnumber of SCoPs: %d\n",
4757              VEC_length (scop_p, current_scops));
4758
4759   for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
4760     {
4761       build_scop_bbs (scop);
4762       build_scop_loop_nests (scop);
4763       build_scop_canonical_schedules (scop);
4764       build_bb_loops (scop);
4765       find_scop_parameters (scop);
4766       build_scop_context (scop);
4767
4768       if (dump_file && (dump_flags & TDF_DETAILS))
4769         {
4770           fprintf (dump_file, "\n(In SCoP %d:\n", i);
4771           fprintf (dump_file, "\nnumber of bbs: %d\n",
4772                    VEC_length (graphite_bb_p, SCOP_BBS (scop)));
4773           fprintf (dump_file, "\nnumber of loops: %d)\n",
4774                    VEC_length (loop_p, SCOP_LOOP_NEST (scop)));
4775         }
4776
4777       if (!build_scop_iteration_domain (scop))
4778         continue;
4779
4780       build_scop_conditions (scop);
4781       build_scop_data_accesses (scop);
4782
4783       if (dump_file && (dump_flags & TDF_DETAILS))
4784         {
4785           int nbrefs = nb_data_refs_in_scop (scop);
4786           fprintf (dump_file, "\nnumber of data refs: %d\n", nbrefs);
4787         }
4788
4789       if (graphite_apply_transformations (scop))
4790         gloog (scop, find_transform (scop));
4791     }
4792
4793   /* Cleanup.  */
4794   free_scops (current_scops);
4795   cloog_finalize ();
4796 }
4797
4798 #else /* If Cloog is not available: #ifndef HAVE_cloog.  */
4799
4800 void
4801 graphite_transform_loops (void)
4802 {
4803   if (dump_file && (dump_flags & TDF_DETAILS))
4804     {
4805       fprintf (dump_file, "Graphite loop optimizations cannot be used.\n");
4806       fprintf (dump_file, "GCC has not been configured with the required "
4807                "libraries for Graphite loop optimizations.");
4808     }
4809   sorry ("Graphite loop optimizations cannot be used");
4810 }
4811
4812 #endif