OSDN Git Service

f169f7252906d0a46bc2a2852c8bf1f7028b18ec
[pf3gnuchains/gcc-fork.git] / gcc / graphite.c
1 /* Gimple Represented as Polyhedra.
2    Copyright (C) 2006, 2007, 2008, 2009 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 "value-prof.h"
55 #include "pointer-set.h"
56 #include "gimple.h"
57
58 #ifdef HAVE_cloog
59 #include "cloog/cloog.h"
60 #include "graphite.h"
61
62 static VEC (scop_p, heap) *current_scops;
63
64 /* Converts a GMP constant V to a tree and returns it.  */
65
66 static tree
67 gmp_cst_to_tree (tree type, Value v)
68 {
69   return build_int_cst (type, value_get_si (v));
70 }
71
72 /* Debug the list of old induction variables for this SCOP.  */
73
74 void
75 debug_oldivs (scop_p scop)
76 {
77   int i;
78   name_tree oldiv;
79
80   fprintf (stderr, "Old IVs:");
81
82   for (i = 0; VEC_iterate (name_tree, SCOP_OLDIVS (scop), i, oldiv); i++)
83     {
84       fprintf (stderr, "(");
85       print_generic_expr (stderr, oldiv->t, 0);
86       fprintf (stderr, ", %s, %d)\n", oldiv->name, oldiv->loop->num);
87     }
88   fprintf (stderr, "\n");
89 }
90
91 /* Debug the loops around basic block GB.  */
92
93 void
94 debug_loop_vec (graphite_bb_p gb)
95 {
96   int i;
97   loop_p loop;
98
99   fprintf (stderr, "Loop Vec:");
100
101   for (i = 0; VEC_iterate (loop_p, GBB_LOOPS (gb), i, loop); i++)
102     fprintf (stderr, "%d: %d, ", i, loop ? loop->num : -1);
103
104   fprintf (stderr, "\n");
105 }
106
107 /* Returns true if stack ENTRY is a constant.  */
108
109 static bool
110 iv_stack_entry_is_constant (iv_stack_entry *entry)
111 {
112   return entry->kind == iv_stack_entry_const;
113 }
114
115 /* Returns true if stack ENTRY is an induction variable.  */
116
117 static bool
118 iv_stack_entry_is_iv (iv_stack_entry *entry)
119 {
120   return entry->kind == iv_stack_entry_iv;
121 }
122
123 /* Push (IV, NAME) on STACK.  */
124
125 static void 
126 loop_iv_stack_push_iv (loop_iv_stack stack, tree iv, const char *name)
127 {
128   iv_stack_entry *entry = XNEW (iv_stack_entry);
129   name_tree named_iv = XNEW (struct name_tree);
130
131   named_iv->t = iv;
132   named_iv->name = name;
133
134   entry->kind = iv_stack_entry_iv;
135   entry->data.iv = named_iv;
136
137   VEC_safe_push (iv_stack_entry_p, heap, *stack, entry);
138 }
139
140 /* Inserts a CONSTANT in STACK at INDEX.  */
141
142 static void
143 loop_iv_stack_insert_constant (loop_iv_stack stack, int index,
144                                tree constant)
145 {
146   iv_stack_entry *entry = XNEW (iv_stack_entry);
147   
148   entry->kind = iv_stack_entry_const;
149   entry->data.constant = constant;
150
151   VEC_safe_insert (iv_stack_entry_p, heap, *stack, index, entry);
152 }
153
154 /* Pops and frees an element out of STACK.  */
155
156 static void
157 loop_iv_stack_pop (loop_iv_stack stack)
158 {
159   iv_stack_entry_p entry = VEC_pop (iv_stack_entry_p, *stack);
160
161   free (entry->data.iv);
162   free (entry);
163 }
164
165 /* Get the IV at INDEX in STACK.  */
166
167 static tree
168 loop_iv_stack_get_iv (loop_iv_stack stack, int index)
169 {
170   iv_stack_entry_p entry = VEC_index (iv_stack_entry_p, *stack, index);
171   iv_stack_entry_data data = entry->data;
172
173   return iv_stack_entry_is_iv (entry) ? data.iv->t : data.constant;
174 }
175
176 /* Get the IV from its NAME in STACK.  */
177
178 static tree
179 loop_iv_stack_get_iv_from_name (loop_iv_stack stack, const char* name)
180 {
181   int i;
182   iv_stack_entry_p entry;
183
184   for (i = 0; VEC_iterate (iv_stack_entry_p, *stack, i, entry); i++)
185     {
186       name_tree iv = entry->data.iv;
187       if (!strcmp (name, iv->name))
188         return iv->t;
189     }
190
191   return NULL;
192 }
193
194 /* Prints on stderr the contents of STACK.  */
195
196 void
197 debug_loop_iv_stack (loop_iv_stack stack)
198 {
199   int i;
200   iv_stack_entry_p entry;
201   bool first = true;
202
203   fprintf (stderr, "(");
204
205   for (i = 0; VEC_iterate (iv_stack_entry_p, *stack, i, entry); i++)
206     {
207       if (first) 
208         first = false;
209       else
210         fprintf (stderr, " ");
211
212       if (iv_stack_entry_is_iv (entry))
213         {
214           name_tree iv = entry->data.iv;
215           fprintf (stderr, "%s:", iv->name);
216           print_generic_expr (stderr, iv->t, 0);
217         }
218       else 
219         {
220           tree constant = entry->data.constant;
221           print_generic_expr (stderr, constant, 0);
222           fprintf (stderr, ":");
223           print_generic_expr (stderr, constant, 0);
224         }
225     }
226
227   fprintf (stderr, ")\n");
228 }
229
230 /* Frees STACK.  */
231
232 static void
233 free_loop_iv_stack (loop_iv_stack stack)
234 {
235   int i;
236   iv_stack_entry_p entry;
237
238   for (i = 0; VEC_iterate (iv_stack_entry_p, *stack, i, entry); i++)
239     {
240       free (entry->data.iv);
241       free (entry);
242     }
243
244   VEC_free (iv_stack_entry_p, heap, *stack);
245 }
246
247 \f
248
249 /* Structure containing the mapping between the CLooG's induction
250    variable and the type of the old induction variable.  */
251 typedef struct ivtype_map_elt
252 {
253   tree type;
254   const char *cloog_iv;
255 } *ivtype_map_elt;
256
257 /* Print to stderr the element ELT.  */
258
259 static void
260 debug_ivtype_elt (ivtype_map_elt elt)
261 {
262   fprintf (stderr, "(%s, ", elt->cloog_iv);
263   print_generic_expr (stderr, elt->type, 0);
264   fprintf (stderr, ")\n");
265 }
266
267 /* Helper function for debug_ivtype_map.  */
268
269 static int
270 debug_ivtype_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
271 {
272   struct ivtype_map_elt *entry = (struct ivtype_map_elt *) *slot;
273   debug_ivtype_elt (entry);
274   return 1;
275 }
276
277 /* Print to stderr all the elements of MAP.  */
278
279 void
280 debug_ivtype_map (htab_t map)
281 {
282   htab_traverse (map, debug_ivtype_map_1, NULL);
283 }
284
285 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW.  */
286
287 static inline ivtype_map_elt
288 new_ivtype_map_elt (const char *cloog_iv, tree type)
289 {
290   ivtype_map_elt res;
291   
292   res = XNEW (struct ivtype_map_elt);
293   res->cloog_iv = cloog_iv;
294   res->type = type;
295
296   return res;
297 }
298
299 /* Computes a hash function for database element ELT.  */
300
301 static hashval_t
302 ivtype_map_elt_info (const void *elt)
303 {
304   return htab_hash_pointer (((const struct ivtype_map_elt *) elt)->cloog_iv);
305 }
306
307 /* Compares database elements E1 and E2.  */
308
309 static int
310 eq_ivtype_map_elts (const void *e1, const void *e2)
311 {
312   const struct ivtype_map_elt *elt1 = (const struct ivtype_map_elt *) e1;
313   const struct ivtype_map_elt *elt2 = (const struct ivtype_map_elt *) e2;
314
315   return (elt1->cloog_iv == elt2->cloog_iv);
316 }
317
318 \f
319
320 /* Given a CLOOG_IV, returns the type that it should have in GCC land.
321    If the information is not available, i.e. in the case one of the
322    transforms created the loop, just return integer_type_node.  */
323
324 static tree
325 gcc_type_for_cloog_iv (const char *cloog_iv, graphite_bb_p gbb)
326 {
327   struct ivtype_map_elt tmp;
328   PTR *slot;
329
330   tmp.cloog_iv = cloog_iv;
331   slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, NO_INSERT);
332
333   if (slot && *slot)
334     return ((ivtype_map_elt) *slot)->type;
335
336   return integer_type_node;
337 }
338
339 /* Inserts constants derived from the USER_STMT argument list into the
340    STACK.  This is needed to map old ivs to constants when loops have
341    been eliminated.  */
342
343 static void 
344 loop_iv_stack_patch_for_consts (loop_iv_stack stack,
345                                 struct clast_user_stmt *user_stmt)
346 {
347   struct clast_stmt *t;
348   int index = 0;
349   CloogStatement *cs = user_stmt->statement;
350   graphite_bb_p gbb = (graphite_bb_p) cloog_statement_usr (cs);
351
352   for (t = user_stmt->substitutions; t; t = t->next) 
353     {
354       struct clast_expr *expr = (struct clast_expr *) 
355         ((struct clast_assignment *)t)->RHS;
356       struct clast_term *term = (struct clast_term *) expr;
357
358       /* FIXME: What should be done with expr_bin, expr_red?  */
359       if (expr->type == expr_term
360           && !term->var)
361         {
362           loop_p loop = gbb_loop_at_index (gbb, index);
363           tree oldiv = oldiv_for_loop (GBB_SCOP (gbb), loop);
364           tree type = oldiv ? TREE_TYPE (oldiv) : integer_type_node;
365           tree value = gmp_cst_to_tree (type, term->val);
366           loop_iv_stack_insert_constant (stack, index, value);
367         }
368       index = index + 1;
369     }
370 }
371
372 /* Removes all constants in the iv STACK.  */
373
374 static void
375 loop_iv_stack_remove_constants (loop_iv_stack stack)
376 {
377   int i;
378   iv_stack_entry *entry;
379   
380   for (i = 0; VEC_iterate (iv_stack_entry_p, *stack, i, entry);)
381     {
382       if (iv_stack_entry_is_constant (entry))
383         {
384           free (VEC_index (iv_stack_entry_p, *stack, i));
385           VEC_ordered_remove (iv_stack_entry_p, *stack, i);
386         }
387       else
388         i++;
389     }
390 }
391
392 /* Returns a new loop_to_cloog_loop_str structure.  */
393
394 static inline struct loop_to_cloog_loop_str *
395 new_loop_to_cloog_loop_str (int loop_num,
396                             int loop_position,
397                             CloogLoop *cloog_loop)
398 {
399   struct loop_to_cloog_loop_str *result;
400
401   result = XNEW (struct loop_to_cloog_loop_str);
402   result->loop_num = loop_num;
403   result->cloog_loop = cloog_loop;
404   result->loop_position = loop_position;
405
406   return result;
407 }
408
409 /* Hash function for SCOP_LOOP2CLOOG_LOOP hash table.  */
410
411 static hashval_t
412 hash_loop_to_cloog_loop (const void *elt)
413 {
414   return ((const struct loop_to_cloog_loop_str *) elt)->loop_num;
415 }
416
417 /* Equality function for SCOP_LOOP2CLOOG_LOOP hash table.  */
418
419 static int
420 eq_loop_to_cloog_loop (const void *el1, const void *el2)
421 {
422   const struct loop_to_cloog_loop_str *elt1, *elt2;
423
424   elt1 = (const struct loop_to_cloog_loop_str *) el1;
425   elt2 = (const struct loop_to_cloog_loop_str *) el2;
426   return elt1->loop_num == elt2->loop_num;
427 }
428
429 /* Compares two graphite bbs and returns an integer less than, equal to, or
430    greater than zero if the first argument is considered to be respectively
431    less than, equal to, or greater than the second. 
432    We compare using the lexicographic order of the static schedules.  */
433
434 static int 
435 gbb_compare (const void *p_1, const void *p_2)
436 {
437   const struct graphite_bb *const gbb_1
438     = *(const struct graphite_bb *const*) p_1;
439   const struct graphite_bb *const gbb_2
440     = *(const struct graphite_bb *const*) p_2;
441
442   return lambda_vector_compare (GBB_STATIC_SCHEDULE (gbb_1),
443                                 gbb_nb_loops (gbb_1) + 1,
444                                 GBB_STATIC_SCHEDULE (gbb_2),
445                                 gbb_nb_loops (gbb_2) + 1);
446 }
447
448 /* Sort graphite bbs in SCOP.  */
449
450 static void
451 graphite_sort_gbbs (scop_p scop)
452 {
453   VEC (graphite_bb_p, heap) *bbs = SCOP_BBS (scop);
454
455   qsort (VEC_address (graphite_bb_p, bbs),
456          VEC_length (graphite_bb_p, bbs),
457          sizeof (graphite_bb_p), gbb_compare);
458 }
459
460 /* Dump conditions of a graphite basic block GBB on FILE.  */
461
462 static void
463 dump_gbb_conditions (FILE *file, graphite_bb_p gbb)
464 {
465   int i;
466   gimple stmt;
467   VEC (gimple, heap) *conditions = GBB_CONDITIONS (gbb);
468   
469   if (VEC_empty (gimple, conditions))
470     return;
471
472   fprintf (file, "\tbb %d\t: cond = {", GBB_BB (gbb)->index);
473
474   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
475     print_gimple_stmt (file, stmt, 0, 0);
476
477   fprintf (file, "}\n");
478 }
479
480 /* Converts the graphite scheduling function into a cloog scattering
481    matrix.  This scattering matrix is used to limit the possible cloog
482    output to valid programs in respect to the scheduling function. 
483
484    SCATTERING_DIMENSIONS specifies the dimensionality of the scattering
485    matrix. CLooG 0.14.0 and previous versions require, that all scattering
486    functions of one CloogProgram have the same dimensionality, therefore we
487    allow to specify it. (Should be removed in future versions)  */
488
489 static CloogMatrix *
490 schedule_to_scattering (graphite_bb_p gb, int scattering_dimensions) 
491 {
492   int i;
493   scop_p scop = GBB_SCOP (gb);
494
495   int nb_iterators = gbb_nb_loops (gb);
496
497   /* The cloog scattering matrix consists of these colums:
498      1                        col  = Eq/Inq,
499      scattering_dimensions    cols = Scattering dimensions,
500      nb_iterators             cols = bb's iterators,
501      scop_nb_params        cols = Parameters,
502      1                        col  = Constant 1.
503
504      Example:
505
506      scattering_dimensions = 5
507      max_nb_iterators = 2
508      nb_iterators = 1 
509      scop_nb_params = 2
510
511      Schedule:
512      ? i
513      4 5
514
515      Scattering Matrix:
516      s1  s2  s3  s4  s5  i   p1  p2  1 
517      1   0   0   0   0   0   0   0  -4  = 0
518      0   1   0   0   0  -1   0   0   0  = 0
519      0   0   1   0   0   0   0   0  -5  = 0  */
520   int nb_params = scop_nb_params (scop);
521   int nb_cols = 1 + scattering_dimensions + nb_iterators + nb_params + 1;
522   int col_const = nb_cols - 1; 
523   int col_iter_offset = 1 + scattering_dimensions;
524
525   CloogMatrix *scat = cloog_matrix_alloc (scattering_dimensions, nb_cols);
526
527   gcc_assert (scattering_dimensions >= nb_iterators * 2 + 1);
528
529   /* Initialize the identity matrix.  */
530   for (i = 0; i < scattering_dimensions; i++)
531     value_set_si (scat->p[i][i + 1], 1);
532
533   /* Textual order outside the first loop */
534   value_set_si (scat->p[0][col_const], -GBB_STATIC_SCHEDULE (gb)[0]);
535
536   /* For all surrounding loops.  */
537   for (i = 0;  i < nb_iterators; i++)
538     {
539       int schedule = GBB_STATIC_SCHEDULE (gb)[i + 1];
540
541       /* Iterations of this loop.  */
542       value_set_si (scat->p[2 * i + 1][col_iter_offset + i], -1);
543
544       /* Textual order inside this loop.  */
545       value_set_si (scat->p[2 * i + 2][col_const], -schedule);
546     }
547
548   return scat; 
549 }
550
551 /* Print the schedules of GB to FILE with INDENT white spaces before.
552    VERBOSITY determines how verbose the code pretty printers are.  */
553
554 void
555 print_graphite_bb (FILE *file, graphite_bb_p gb, int indent, int verbosity)
556 {
557   CloogMatrix *scattering;
558   int i;
559   loop_p loop;
560   fprintf (file, "\nGBB (\n");
561
562   print_loops_bb (file, GBB_BB (gb), indent+2, verbosity);
563
564   if (GBB_DOMAIN (gb))
565     {
566       fprintf (file, "       (domain: \n");
567       cloog_matrix_print (file, GBB_DOMAIN (gb));
568       fprintf (file, "       )\n");
569     }
570
571   if (GBB_STATIC_SCHEDULE (gb))
572     {
573       fprintf (file, "       (static schedule: ");
574       print_lambda_vector (file, GBB_STATIC_SCHEDULE (gb),
575                            gbb_nb_loops (gb) + 1);
576       fprintf (file, "       )\n");
577     }
578
579   if (GBB_LOOPS (gb))
580     {
581       fprintf (file, "       (contained loops: \n");
582       for (i = 0; VEC_iterate (loop_p, GBB_LOOPS (gb), i, loop); i++)
583         if (loop == NULL)
584           fprintf (file, "       iterator %d   =>  NULL \n", i); 
585         else
586           fprintf (file, "       iterator %d   =>  loop %d \n", i,
587                    loop->num);
588       fprintf (file, "       )\n");
589     }
590
591   if (GBB_DATA_REFS (gb))
592     dump_data_references (file, GBB_DATA_REFS (gb));
593
594   if (GBB_CONDITIONS (gb))
595     {
596       fprintf (file, "       (conditions: \n");
597       dump_gbb_conditions (file, gb);
598       fprintf (file, "       )\n");
599     }
600
601   if (GBB_SCOP (gb)
602       && GBB_STATIC_SCHEDULE (gb))
603     {
604       fprintf (file, "       (scattering: \n");
605       scattering = schedule_to_scattering (gb, 2 * gbb_nb_loops (gb) + 1);
606       cloog_matrix_print (file, scattering);
607       cloog_matrix_free (scattering);
608       fprintf (file, "       )\n");
609     }
610
611   fprintf (file, ")\n");
612 }
613
614 /* Print to STDERR the schedules of GB with VERBOSITY level.  */
615
616 void
617 debug_gbb (graphite_bb_p gb, int verbosity)
618 {
619   print_graphite_bb (stderr, gb, 0, verbosity);
620 }
621
622
623 /* Print SCOP to FILE.  VERBOSITY determines how verbose the pretty
624    printers are.  */
625
626 static void
627 print_scop (FILE *file, scop_p scop, int verbosity)
628 {
629   if (scop == NULL)
630     return;
631
632   fprintf (file, "\nSCoP_%d_%d (\n",
633            SCOP_ENTRY (scop)->index, SCOP_EXIT (scop)->index);
634
635   fprintf (file, "       (cloog: \n");
636   cloog_program_print (file, SCOP_PROG (scop));
637   fprintf (file, "       )\n");
638
639   if (SCOP_BBS (scop))
640     {
641       graphite_bb_p gb;
642       int i;
643
644       for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
645         print_graphite_bb (file, gb, 0, verbosity);
646     }
647
648   fprintf (file, ")\n");
649 }
650
651 /* Print all the SCOPs to FILE.  VERBOSITY determines how verbose the
652    code pretty printers are.  */
653
654 static void
655 print_scops (FILE *file, int verbosity)
656 {
657   int i;
658   scop_p scop;
659
660   for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
661     print_scop (file, scop, verbosity);
662 }
663
664 /* Debug SCOP.  VERBOSITY determines how verbose the code pretty
665    printers are. */
666
667 void
668 debug_scop (scop_p scop, int verbosity)
669 {
670   print_scop (stderr, scop, verbosity);
671 }
672
673 /* Debug all SCOPs from CURRENT_SCOPS.  VERBOSITY determines how
674    verbose the code pretty printers are.  */
675
676 void 
677 debug_scops (int verbosity)
678 {
679   print_scops (stderr, verbosity);
680 }
681
682 /* Pretty print to FILE the SCOP in DOT format.  */
683
684 static void 
685 dot_scop_1 (FILE *file, scop_p scop)
686 {
687   edge e;
688   edge_iterator ei;
689   basic_block bb;
690   basic_block entry = SCOP_ENTRY (scop);
691   basic_block exit = SCOP_EXIT (scop);
692     
693   fprintf (file, "digraph SCoP_%d_%d {\n", entry->index,
694            exit->index);
695
696   FOR_ALL_BB (bb)
697     {
698       if (bb == entry)
699         fprintf (file, "%d [shape=triangle];\n", bb->index);
700
701       if (bb == exit)
702         fprintf (file, "%d [shape=box];\n", bb->index);
703
704       if (bb_in_scop_p (bb, scop)) 
705         fprintf (file, "%d [color=red];\n", bb->index);
706
707       FOR_EACH_EDGE (e, ei, bb->succs)
708         fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
709     }
710
711   fputs ("}\n\n", file);
712 }
713
714 /* Display SCOP using dotty.  */
715
716 void
717 dot_scop (scop_p scop)
718 {
719   dot_scop_1 (stderr, scop);
720 }
721
722 /* Pretty print all SCoPs in DOT format and mark them with different colors.
723    If there are not enough colors, paint later SCoPs gray.
724    Special nodes:
725    - "*" after the node number: entry of a SCoP,
726    - "#" after the node number: exit of a SCoP,
727    - "()" entry or exit not part of SCoP.  */
728
729 static void
730 dot_all_scops_1 (FILE *file)
731 {
732   basic_block bb;
733   edge e;
734   edge_iterator ei;
735   scop_p scop;
736   const char* color;
737   int i;
738
739   /* Disable debugging while printing graph.  */
740   int tmp_dump_flags = dump_flags;
741   dump_flags = 0;
742
743   fprintf (file, "digraph all {\n");
744
745   FOR_ALL_BB (bb)
746     {
747       int part_of_scop = false;
748
749       /* Use HTML for every bb label.  So we are able to print bbs
750          which are part of two different SCoPs, with two different
751          background colors.  */
752       fprintf (file, "%d [label=<\n  <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
753                      bb->index);
754       fprintf (file, "CELLSPACING=\"0\">\n");
755
756       /* Select color for SCoP.  */
757       for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
758         if (bb_in_scop_p (bb, scop)
759             || (SCOP_EXIT (scop) == bb)
760             || (SCOP_ENTRY (scop) == bb))
761           {
762             switch (i % 17)
763               {
764               case 0: /* red */
765                 color = "#e41a1c";
766                 break;
767               case 1: /* blue */
768                 color = "#377eb8";
769                 break;
770               case 2: /* green */
771                 color = "#4daf4a";
772                 break;
773               case 3: /* purple */
774                 color = "#984ea3";
775                 break;
776               case 4: /* orange */
777                 color = "#ff7f00";
778                 break;
779               case 5: /* yellow */
780                 color = "#ffff33";
781                 break;
782               case 6: /* brown */
783                 color = "#a65628";
784                 break;
785               case 7: /* rose */
786                 color = "#f781bf";
787                 break;
788               case 8:
789                 color = "#8dd3c7";
790                 break;
791               case 9:
792                 color = "#ffffb3";
793                 break;
794               case 10:
795                 color = "#bebada";
796                 break;
797               case 11:
798                 color = "#fb8072";
799                 break;
800               case 12:
801                 color = "#80b1d3";
802                 break;
803               case 13:
804                 color = "#fdb462";
805                 break;
806               case 14:
807                 color = "#b3de69";
808                 break;
809               case 15:
810                 color = "#fccde5";
811                 break;
812               case 16:
813                 color = "#bc80bd";
814                 break;
815               default: /* gray */
816                 color = "#999999";
817               }
818
819             fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
820         
821             if (!bb_in_scop_p (bb, scop))
822               fprintf (file, " ("); 
823
824             if (bb == SCOP_ENTRY (scop)
825                 && bb == SCOP_EXIT (scop))
826               fprintf (file, " %d*# ", bb->index);
827             else if (bb == SCOP_ENTRY (scop))
828               fprintf (file, " %d* ", bb->index);
829             else if (bb == SCOP_EXIT (scop))
830               fprintf (file, " %d# ", bb->index);
831             else
832               fprintf (file, " %d ", bb->index);
833
834             if (!bb_in_scop_p (bb, scop))
835               fprintf (file, ")");
836
837             fprintf (file, "</TD></TR>\n");
838             part_of_scop  = true;
839           }
840
841       if (!part_of_scop)
842         {
843           fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
844           fprintf (file, " %d </TD></TR>\n", bb->index);
845         }
846
847       fprintf (file, "  </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
848     }
849
850   FOR_ALL_BB (bb)
851     {
852       FOR_EACH_EDGE (e, ei, bb->succs)
853               fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
854     }
855
856   fputs ("}\n\n", file);
857
858   /* Enable debugging again.  */
859   dump_flags = tmp_dump_flags;
860 }
861
862 /* Display all SCoPs using dotty.  */
863
864 void
865 dot_all_scops (void)
866 {
867   /* When debugging, enable the following code.  This cannot be used
868      in production compilers because it calls "system".  */
869 #if 0
870   FILE *stream = fopen ("/tmp/allscops.dot", "w");
871   gcc_assert (stream);
872
873   dot_all_scops_1 (stream);
874   fclose (stream);
875
876   system ("dotty /tmp/allscops.dot");
877 #else
878   dot_all_scops_1 (stderr);
879 #endif
880 }
881
882 /* Returns the outermost loop in SCOP that contains BB.  */
883
884 static struct loop *
885 outermost_loop_in_scop (scop_p scop, basic_block bb)
886 {
887   struct loop *nest;
888
889   nest = bb->loop_father;
890   while (loop_outer (nest) && loop_in_scop_p (loop_outer (nest), scop))
891     nest = loop_outer (nest);
892
893   return nest;
894 }
895
896 /* Returns the block preceding the entry of SCOP.  */
897
898 static basic_block
899 block_before_scop (scop_p scop)
900 {
901   return SESE_ENTRY (SCOP_REGION (scop))->src;
902 }
903
904 /* Return true when EXPR is an affine function in LOOP with parameters
905    instantiated relative to SCOP_ENTRY.  */
906
907 static bool
908 loop_affine_expr (basic_block scop_entry, struct loop *loop, tree expr)
909 {
910   int n = loop->num;
911   tree scev = analyze_scalar_evolution (loop, expr);
912
913   scev = instantiate_scev (scop_entry, loop, scev);
914
915   return (evolution_function_is_invariant_p (scev, n)
916           || evolution_function_is_affine_multivariate_p (scev, n));
917 }
918
919 /* Return false if the tree_code of the operand OP or any of its operands
920    is component_ref.  */
921
922 static bool
923 exclude_component_ref (tree op) 
924 {
925   int i;
926   int len;
927
928   if (op)
929     {
930       if (TREE_CODE (op) == COMPONENT_REF)
931         return false;
932       else
933         {
934           len = TREE_OPERAND_LENGTH (op);         
935           for (i = 0; i < len; ++i)
936             {
937               if (!exclude_component_ref (TREE_OPERAND (op, i)))
938                 return false;
939             }
940         }
941     }
942
943   return true;
944 }
945
946 /* Return true if the operand OP is simple.  */
947
948 static bool
949 is_simple_operand (loop_p loop, gimple stmt, tree op) 
950 {
951   /* It is not a simple operand when it is a declaration,  */
952   if (DECL_P (op)
953       /* or a structure,  */
954       || AGGREGATE_TYPE_P (TREE_TYPE (op))
955       /* or a memory access that cannot be analyzed by the data
956          reference analysis.  */
957       || ((handled_component_p (op) || INDIRECT_REF_P (op))
958           && !stmt_simple_memref_p (loop, stmt, op)))
959     return false;
960
961   return exclude_component_ref (op);
962 }
963
964 /* Return true only when STMT is simple enough for being handled by
965    Graphite.  This depends on SCOP_ENTRY, as the parametetrs are
966    initialized relatively to this basic block.  */
967
968 static bool
969 stmt_simple_for_scop_p (basic_block scop_entry, gimple stmt)
970 {
971   basic_block bb = gimple_bb (stmt);
972   struct loop *loop = bb->loop_father;
973
974   /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
975      Calls have side-effects, except those to const or pure
976      functions.  */
977   if (gimple_has_volatile_ops (stmt)
978       || (gimple_code (stmt) == GIMPLE_CALL
979           && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
980       || (gimple_code (stmt) == GIMPLE_ASM))
981     return false;
982
983   switch (gimple_code (stmt))
984     {
985     case GIMPLE_RETURN:
986     case GIMPLE_LABEL:
987       return true;
988
989     case GIMPLE_COND:
990       {
991         tree op;
992         ssa_op_iter op_iter;
993         enum tree_code code = gimple_cond_code (stmt);
994
995         /* We can only handle this kind of conditional expressions.  
996            For inequalities like "if (i != 3 * k)" we need unions of
997            polyhedrons.  Expressions like  "if (a)" or "if (a == 15)" need
998            them for the else branch.  */
999         if (!(code == LT_EXPR
1000               || code == GT_EXPR
1001               || code == LE_EXPR
1002               || code == GE_EXPR))
1003           return false;
1004
1005         if (!scop_entry)
1006           return false;
1007
1008         FOR_EACH_SSA_TREE_OPERAND (op, stmt, op_iter, SSA_OP_ALL_USES)
1009           if (!loop_affine_expr (scop_entry, loop, op))
1010             return false;
1011
1012         return true;
1013       }
1014
1015     case GIMPLE_ASSIGN:
1016       {
1017         enum tree_code code = gimple_assign_rhs_code (stmt);
1018
1019         switch (get_gimple_rhs_class (code))
1020           {
1021           case GIMPLE_UNARY_RHS:
1022           case GIMPLE_SINGLE_RHS:
1023             return (is_simple_operand (loop, stmt, gimple_assign_lhs (stmt))
1024                     && is_simple_operand (loop, stmt, gimple_assign_rhs1 (stmt)));
1025
1026           case GIMPLE_BINARY_RHS:
1027             return (is_simple_operand (loop, stmt, gimple_assign_lhs (stmt))
1028                     && is_simple_operand (loop, stmt, gimple_assign_rhs1 (stmt))
1029                     && is_simple_operand (loop, stmt, gimple_assign_rhs2 (stmt)));
1030
1031           case GIMPLE_INVALID_RHS:
1032           default:
1033             gcc_unreachable ();
1034           }
1035       }
1036
1037     case GIMPLE_CALL:
1038       {
1039         size_t i;
1040         size_t n = gimple_call_num_args (stmt);
1041         tree lhs = gimple_call_lhs (stmt);
1042
1043         if (lhs && !is_simple_operand (loop, stmt, lhs))
1044           return false;
1045
1046         for (i = 0; i < n; i++)
1047           if (!is_simple_operand (loop, stmt, gimple_call_arg (stmt, i)))
1048             return false;
1049
1050         return true;
1051       }
1052
1053     default:
1054       /* These nodes cut a new scope.  */
1055       return false;
1056     }
1057
1058   return false;
1059 }
1060
1061 /* Returns the statement of BB that contains a harmful operation: that
1062    can be a function call with side effects, the induction variables
1063    are not linear with respect to SCOP_ENTRY, etc.  The current open
1064    scop should end before this statement.  */
1065
1066 static gimple
1067 harmful_stmt_in_bb (basic_block scop_entry, basic_block bb)
1068 {
1069   gimple_stmt_iterator gsi;
1070
1071   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1072     if (!stmt_simple_for_scop_p (scop_entry, gsi_stmt (gsi)))
1073       return gsi_stmt (gsi);
1074
1075   return NULL;
1076 }
1077
1078 /* Returns true when BB will be represented in graphite.  Return false
1079    for the basic blocks that contain code eliminated in the code
1080    generation pass: i.e. induction variables and exit conditions.  */
1081
1082 static bool
1083 graphite_stmt_p (scop_p scop, basic_block bb,
1084                  VEC (data_reference_p, heap) *drs)
1085 {
1086   gimple_stmt_iterator gsi;
1087   loop_p loop = bb->loop_father;
1088
1089   if (VEC_length (data_reference_p, drs) > 0)
1090     return true;
1091
1092   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1093     {
1094       gimple stmt = gsi_stmt (gsi);
1095
1096       switch (gimple_code (stmt))
1097         {
1098           /* Control flow expressions can be ignored, as they are
1099              represented in the iteration domains and will be
1100              regenerated by graphite.  */
1101         case GIMPLE_COND:
1102         case GIMPLE_GOTO:
1103         case GIMPLE_SWITCH:
1104           break;
1105
1106         case GIMPLE_ASSIGN:
1107           {
1108             tree var = gimple_assign_lhs (stmt);
1109             var = analyze_scalar_evolution (loop, var);
1110             var = instantiate_scev (block_before_scop (scop), loop, var);
1111
1112             if (chrec_contains_undetermined (var))
1113               return true;
1114
1115             break;
1116           }
1117
1118         default:
1119           return true;
1120         }
1121     }
1122
1123   return false;
1124 }
1125
1126 /* Store the GRAPHITE representation of BB.  */
1127
1128 static void
1129 new_graphite_bb (scop_p scop, basic_block bb)
1130 {
1131   struct graphite_bb *gbb;
1132   VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
1133   struct loop *nest = outermost_loop_in_scop (scop, bb);
1134   gimple_stmt_iterator gsi;
1135
1136   bitmap_set_bit (SCOP_BBS_B (scop), bb->index);
1137
1138   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1139     find_data_references_in_stmt (nest, gsi_stmt (gsi), &drs);
1140
1141   if (!graphite_stmt_p (scop, bb, drs))
1142     {
1143       free_data_refs (drs);
1144       return;
1145     }
1146
1147   gbb = XNEW (struct graphite_bb);
1148   bb->aux = gbb;
1149   GBB_BB (gbb) = bb;
1150   GBB_SCOP (gbb) = scop;
1151   GBB_DATA_REFS (gbb) = drs;
1152   GBB_DOMAIN (gbb) = NULL;
1153   GBB_CONDITIONS (gbb) = NULL;
1154   GBB_CONDITION_CASES (gbb) = NULL;
1155   GBB_LOOPS (gbb) = NULL;
1156   GBB_STATIC_SCHEDULE (gbb) = NULL;
1157   GBB_CLOOG_IV_TYPES (gbb) = NULL;
1158   VEC_safe_push (graphite_bb_p, heap, SCOP_BBS (scop), gbb);
1159 }
1160
1161 /* Frees GBB.  */
1162
1163 static void
1164 free_graphite_bb (struct graphite_bb *gbb)
1165 {
1166   if (GBB_DOMAIN (gbb))
1167     cloog_matrix_free (GBB_DOMAIN (gbb));
1168
1169   if (GBB_CLOOG_IV_TYPES (gbb))
1170     htab_delete (GBB_CLOOG_IV_TYPES (gbb));
1171
1172   /* FIXME: free_data_refs is disabled for the moment, but should be
1173      enabled.
1174
1175      free_data_refs (GBB_DATA_REFS (gbb)); */
1176
1177   VEC_free (gimple, heap, GBB_CONDITIONS (gbb));
1178   VEC_free (gimple, heap, GBB_CONDITION_CASES (gbb));
1179   VEC_free (loop_p, heap, GBB_LOOPS (gbb));
1180   GBB_BB (gbb)->aux = 0;
1181   XDELETE (gbb);
1182 }
1183
1184 \f
1185
1186 /* Structure containing the mapping between the old names and the new
1187    names used after block copy in the new loop context.  */
1188 typedef struct rename_map_elt
1189 {
1190   tree old_name, new_name;
1191 } *rename_map_elt;
1192
1193
1194 /* Print to stderr the element ELT.  */
1195
1196 static void
1197 debug_rename_elt (rename_map_elt elt)
1198 {
1199   fprintf (stderr, "(");
1200   print_generic_expr (stderr, elt->old_name, 0);
1201   fprintf (stderr, ", ");
1202   print_generic_expr (stderr, elt->new_name, 0);
1203   fprintf (stderr, ")\n");
1204 }
1205
1206 /* Helper function for debug_rename_map.  */
1207
1208 static int
1209 debug_rename_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
1210 {
1211   struct rename_map_elt *entry = (struct rename_map_elt *) *slot;
1212   debug_rename_elt (entry);
1213   return 1;
1214 }
1215
1216 /* Print to stderr all the elements of MAP.  */
1217
1218 void
1219 debug_rename_map (htab_t map)
1220 {
1221   htab_traverse (map, debug_rename_map_1, NULL);
1222 }
1223
1224 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW.  */
1225
1226 static inline rename_map_elt
1227 new_rename_map_elt (tree old_name, tree new_name)
1228 {
1229   rename_map_elt res;
1230   
1231   res = XNEW (struct rename_map_elt);
1232   res->old_name = old_name;
1233   res->new_name = new_name;
1234
1235   return res;
1236 }
1237
1238 /* Computes a hash function for database element ELT.  */
1239
1240 static hashval_t
1241 rename_map_elt_info (const void *elt)
1242 {
1243   return htab_hash_pointer (((const struct rename_map_elt *) elt)->old_name);
1244 }
1245
1246 /* Compares database elements E1 and E2.  */
1247
1248 static int
1249 eq_rename_map_elts (const void *e1, const void *e2)
1250 {
1251   const struct rename_map_elt *elt1 = (const struct rename_map_elt *) e1;
1252   const struct rename_map_elt *elt2 = (const struct rename_map_elt *) e2;
1253
1254   return (elt1->old_name == elt2->old_name);
1255 }
1256
1257 /* Returns the new name associated to OLD_NAME in MAP.  */
1258
1259 static tree
1260 get_new_name_from_old_name (htab_t map, tree old_name)
1261 {
1262   struct rename_map_elt tmp;
1263   PTR *slot;
1264
1265   tmp.old_name = old_name;
1266   slot = htab_find_slot (map, &tmp, NO_INSERT);
1267
1268   if (slot && *slot)
1269     return ((rename_map_elt) *slot)->new_name;
1270
1271   return old_name;
1272 }
1273
1274 \f
1275
1276 /* Returns true when BB is in REGION.  */
1277
1278 static bool
1279 bb_in_sese_p (basic_block bb, sese region)
1280 {
1281   return pointer_set_contains (SESE_REGION_BBS (region), bb);
1282 }
1283
1284 /* For a USE in BB, if BB is outside REGION, mark the USE in the
1285    SESE_LIVEIN and SESE_LIVEOUT sets.  */
1286
1287 static void
1288 sese_build_livein_liveouts_use (sese region, basic_block bb, tree use)
1289 {
1290   unsigned ver;
1291   basic_block def_bb;
1292
1293   if (TREE_CODE (use) != SSA_NAME)
1294     return;
1295
1296   ver = SSA_NAME_VERSION (use);
1297   def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
1298   if (!def_bb
1299       || !bb_in_sese_p (def_bb, region)
1300       || bb_in_sese_p (bb, region))
1301     return;
1302
1303   if (!SESE_LIVEIN_VER (region, ver))
1304     SESE_LIVEIN_VER (region, ver) = BITMAP_ALLOC (NULL);
1305
1306   bitmap_set_bit (SESE_LIVEIN_VER (region, ver), bb->index);
1307   bitmap_set_bit (SESE_LIVEOUT (region), ver);
1308 }
1309
1310 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
1311    used in BB that is outside of the REGION.  */
1312
1313 static void
1314 sese_build_livein_liveouts_bb (sese region, basic_block bb)
1315 {
1316   gimple_stmt_iterator bsi;
1317   edge e;
1318   edge_iterator ei;
1319   ssa_op_iter iter;
1320   tree var;
1321
1322   FOR_EACH_EDGE (e, ei, bb->succs)
1323     for (bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi); gsi_next (&bsi))
1324       sese_build_livein_liveouts_use (region, bb,
1325                                       PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi), e));
1326
1327   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1328     FOR_EACH_SSA_TREE_OPERAND (var, gsi_stmt (bsi), iter, SSA_OP_ALL_USES)
1329       sese_build_livein_liveouts_use (region, bb, var);
1330 }
1331
1332 /* Build the SESE_LIVEIN and SESE_LIVEOUT for REGION.  */
1333
1334 void
1335 sese_build_livein_liveouts (sese region)
1336 {
1337   basic_block bb;
1338
1339   SESE_LIVEOUT (region) = BITMAP_ALLOC (NULL);
1340   SESE_NUM_VER (region) = num_ssa_names;
1341   SESE_LIVEIN (region) = XCNEWVEC (bitmap, SESE_NUM_VER (region));
1342
1343   FOR_EACH_BB (bb)
1344     sese_build_livein_liveouts_bb (region, bb);
1345 }
1346
1347 /* Register basic blocks belonging to a region in a pointer set.  */
1348
1349 static void
1350 register_bb_in_sese (basic_block entry_bb, basic_block exit_bb, sese region)
1351 {
1352   edge_iterator ei;
1353   edge e;
1354   basic_block bb = entry_bb;
1355
1356   FOR_EACH_EDGE (e, ei, bb->succs)
1357     {
1358       if (!pointer_set_contains (SESE_REGION_BBS (region), e->dest) &&
1359           e->dest->index != exit_bb->index)
1360         {       
1361           pointer_set_insert (SESE_REGION_BBS (region), e->dest);
1362           register_bb_in_sese (e->dest, exit_bb, region);
1363         }
1364     }
1365 }
1366
1367 /* Builds a new SESE region from edges ENTRY and EXIT.  */
1368
1369 sese
1370 new_sese (edge entry, edge exit)
1371 {
1372   sese res = XNEW (struct sese);
1373
1374   SESE_ENTRY (res) = entry;
1375   SESE_EXIT (res) = exit;
1376   SESE_REGION_BBS (res) = pointer_set_create ();
1377   register_bb_in_sese (entry->dest, exit->dest, res);
1378
1379   SESE_LIVEOUT (res) = NULL;
1380   SESE_NUM_VER (res) = 0;
1381   SESE_LIVEIN (res) = NULL;
1382
1383   return res;
1384 }
1385
1386 /* Deletes REGION.  */
1387
1388 void
1389 free_sese (sese region)
1390 {
1391   int i;
1392
1393   for (i = 0; i < SESE_NUM_VER (region); i++)
1394     BITMAP_FREE (SESE_LIVEIN_VER (region, i));
1395
1396   if (SESE_LIVEIN (region))
1397     free (SESE_LIVEIN (region));
1398
1399   if (SESE_LIVEOUT (region))
1400     BITMAP_FREE (SESE_LIVEOUT (region));
1401
1402   pointer_set_destroy (SESE_REGION_BBS (region));
1403   XDELETE (region);
1404 }
1405
1406 \f
1407
1408 /* Creates a new scop starting with ENTRY.  */
1409
1410 static scop_p
1411 new_scop (edge entry, edge exit)
1412 {
1413   scop_p scop = XNEW (struct scop);
1414
1415   gcc_assert (entry && exit);
1416
1417   SCOP_REGION (scop) = new_sese (entry, exit);
1418   SCOP_BBS (scop) = VEC_alloc (graphite_bb_p, heap, 3);
1419   SCOP_OLDIVS (scop) = VEC_alloc (name_tree, heap, 3);
1420   SCOP_BBS_B (scop) = BITMAP_ALLOC (NULL);
1421   SCOP_LOOPS (scop) = BITMAP_ALLOC (NULL);
1422   SCOP_LOOP_NEST (scop) = VEC_alloc (loop_p, heap, 3);
1423   SCOP_ADD_PARAMS (scop) = true;
1424   SCOP_PARAMS (scop) = VEC_alloc (name_tree, heap, 3);
1425   SCOP_PROG (scop) = cloog_program_malloc ();
1426   cloog_program_set_names (SCOP_PROG (scop), cloog_names_malloc ());
1427   SCOP_LOOP2CLOOG_LOOP (scop) = htab_create (10, hash_loop_to_cloog_loop,
1428                                              eq_loop_to_cloog_loop,
1429                                              free);
1430   SCOP_LIVEOUT_RENAMES (scop) = htab_create (10, rename_map_elt_info,
1431                                              eq_rename_map_elts, free);
1432   return scop;
1433 }
1434
1435 /* Deletes SCOP.  */
1436
1437 static void
1438 free_scop (scop_p scop)
1439 {
1440   int i;
1441   name_tree p;
1442   struct graphite_bb *gb;
1443   name_tree iv;
1444
1445   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
1446     free_graphite_bb (gb);
1447
1448   VEC_free (graphite_bb_p, heap, SCOP_BBS (scop));
1449   BITMAP_FREE (SCOP_BBS_B (scop));
1450   BITMAP_FREE (SCOP_LOOPS (scop));
1451   VEC_free (loop_p, heap, SCOP_LOOP_NEST (scop));
1452
1453   for (i = 0; VEC_iterate (name_tree, SCOP_OLDIVS (scop), i, iv); i++)
1454     free (iv);
1455   VEC_free (name_tree, heap, SCOP_OLDIVS (scop));
1456   
1457   for (i = 0; VEC_iterate (name_tree, SCOP_PARAMS (scop), i, p); i++)
1458     free (p);
1459
1460   VEC_free (name_tree, heap, SCOP_PARAMS (scop));
1461   cloog_program_free (SCOP_PROG (scop));
1462   htab_delete (SCOP_LOOP2CLOOG_LOOP (scop)); 
1463   htab_delete (SCOP_LIVEOUT_RENAMES (scop));
1464   free_sese (SCOP_REGION (scop));
1465   XDELETE (scop);
1466 }
1467
1468 /* Deletes all scops in SCOPS.  */
1469
1470 static void
1471 free_scops (VEC (scop_p, heap) *scops)
1472 {
1473   int i;
1474   scop_p scop;
1475
1476   for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
1477     free_scop (scop);
1478
1479   VEC_free (scop_p, heap, scops);
1480 }
1481
1482 typedef enum gbb_type {
1483   GBB_UNKNOWN,
1484   GBB_LOOP_SING_EXIT_HEADER,
1485   GBB_LOOP_MULT_EXIT_HEADER,
1486   GBB_LOOP_EXIT,
1487   GBB_COND_HEADER,
1488   GBB_SIMPLE,
1489   GBB_LAST
1490 } gbb_type;
1491
1492 /* Detect the type of BB.  Loop headers are only marked, if they are
1493    new.  This means their loop_father is different to LAST_LOOP.
1494    Otherwise they are treated like any other bb and their type can be
1495    any other type.  */
1496
1497 static gbb_type
1498 get_bb_type (basic_block bb, struct loop *last_loop)
1499 {
1500   VEC (basic_block, heap) *dom;
1501   int nb_dom, nb_suc;
1502   struct loop *loop = bb->loop_father;
1503
1504   /* Check, if we entry into a new loop. */
1505   if (loop != last_loop)
1506     {
1507       if (single_exit (loop) != NULL)
1508         return GBB_LOOP_SING_EXIT_HEADER;
1509       else if (loop->num != 0)
1510         return GBB_LOOP_MULT_EXIT_HEADER;
1511       else
1512         return GBB_COND_HEADER;
1513     }
1514
1515   dom = get_dominated_by (CDI_DOMINATORS, bb);
1516   nb_dom = VEC_length (basic_block, dom);
1517   VEC_free (basic_block, heap, dom);
1518
1519   if (nb_dom == 0)
1520     return GBB_LAST;
1521
1522   nb_suc = VEC_length (edge, bb->succs);
1523
1524   if (nb_dom == 1 && nb_suc == 1)
1525     return GBB_SIMPLE;
1526
1527   return GBB_COND_HEADER;
1528 }
1529
1530 /* A SCoP detection region, defined using bbs as borders. 
1531    All control flow touching this region, comes in passing basic_block ENTRY and
1532    leaves passing basic_block EXIT.  By using bbs instead of edges for the
1533    borders we are able to represent also regions that do not have a single
1534    entry or exit edge.
1535    But as they have a single entry basic_block and a single exit basic_block, we
1536    are able to generate for every sd_region a single entry and exit edge.
1537
1538    1   2
1539     \ /
1540      3  <- entry
1541      |
1542      4
1543     / \                 This region contains: {3, 4, 5, 6, 7, 8}
1544    5   6
1545    |   |
1546    7   8
1547     \ /
1548      9  <- exit  */
1549
1550
1551 typedef struct sd_region_p
1552 {
1553   /* The entry bb dominates all bbs in the sd_region.  It is part of the
1554      region.  */
1555   basic_block entry;
1556
1557   /* The exit bb postdominates all bbs in the sd_region, but is not 
1558      part of the region.  */
1559   basic_block exit;
1560 } sd_region;
1561
1562 DEF_VEC_O(sd_region);
1563 DEF_VEC_ALLOC_O(sd_region, heap);
1564
1565
1566 /* Moves the scops from SOURCE to TARGET and clean up SOURCE.  */
1567
1568 static void
1569 move_sd_regions (VEC (sd_region, heap) **source, VEC (sd_region, heap) **target)
1570 {
1571   sd_region *s;
1572   int i;
1573
1574   for (i = 0; VEC_iterate (sd_region, *source, i, s); i++)
1575     VEC_safe_push (sd_region, heap, *target, s);
1576   
1577   VEC_free (sd_region, heap, *source);
1578 }
1579
1580 /* Return true when it is not possible to represent the upper bound of
1581    LOOP in the polyhedral representation.  */
1582
1583 static bool
1584 graphite_cannot_represent_loop_niter (loop_p loop)
1585 {
1586   tree niter = number_of_latch_executions (loop);
1587
1588   return chrec_contains_undetermined (niter)
1589     || !scev_is_linear_expression (niter);
1590 }
1591 /* Store information needed by scopdet_* functions.  */
1592
1593 struct scopdet_info
1594 {
1595   /* Where the last open scop would stop if the current BB is harmful.  */
1596   basic_block last;
1597
1598   /* Where the next scop would start if the current BB is harmful.  */
1599   basic_block next;
1600
1601   /* The bb or one of its children contains open loop exits.  That means
1602      loop exit nodes that are not surrounded by a loop dominated by bb.  */ 
1603   bool exits;
1604
1605   /* The bb or one of its children contains only structures we can handle.  */ 
1606   bool difficult;
1607 };
1608
1609
1610 static struct scopdet_info build_scops_1 (basic_block, VEC (sd_region, heap) **,
1611                                           loop_p);
1612
1613 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
1614    to SCOPS.  TYPE is the gbb_type of BB.  */
1615
1616 static struct scopdet_info 
1617 scopdet_basic_block_info (basic_block bb, VEC (sd_region, heap) **scops,
1618                           gbb_type type)
1619 {
1620   struct loop *loop = bb->loop_father;
1621   struct scopdet_info result;
1622   gimple stmt;
1623
1624   /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps.  */
1625   stmt = harmful_stmt_in_bb (ENTRY_BLOCK_PTR, bb);
1626   result.difficult = (stmt != NULL);
1627   result.last = NULL;
1628
1629   switch (type)
1630     {
1631     case GBB_LAST:
1632       result.next = NULL;
1633       result.exits = false;
1634       result.last = bb;
1635       break;
1636
1637     case GBB_SIMPLE:
1638       result.next = single_succ (bb);
1639       result.exits = false;
1640       result.last = bb;
1641       break;
1642
1643     case GBB_LOOP_SING_EXIT_HEADER:
1644       {
1645         VEC (sd_region, heap) *tmp_scops = VEC_alloc (sd_region, heap,3);
1646         struct scopdet_info sinfo;
1647
1648         sinfo = build_scops_1 (bb, &tmp_scops, loop);
1649         
1650         result.last = single_exit (bb->loop_father)->src;
1651         result.next = single_exit (bb->loop_father)->dest;
1652
1653         /* If we do not dominate result.next, remove it.  It's either
1654            the EXIT_BLOCK_PTR, or another bb dominates it and will
1655            call the scop detection for this bb.  */
1656         if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
1657           result.next = NULL;
1658
1659         if (result.last->loop_father != loop)
1660           result.next = NULL;
1661
1662         if (graphite_cannot_represent_loop_niter (loop))
1663           result.difficult = true;
1664
1665         if (sinfo.difficult)
1666           move_sd_regions (&tmp_scops, scops);
1667         else 
1668           VEC_free (sd_region, heap, tmp_scops);
1669
1670         result.exits = false;
1671         result.difficult |= sinfo.difficult;
1672         break;
1673       }
1674
1675     case GBB_LOOP_MULT_EXIT_HEADER:
1676       {
1677         /* XXX: For now we just do not join loops with multiple exits.  If the 
1678            exits lead to the same bb it may be possible to join the loop.  */
1679         VEC (sd_region, heap) *tmp_scops = VEC_alloc (sd_region, heap, 3);
1680         VEC (edge, heap) *exits = get_loop_exit_edges (loop);
1681         edge e;
1682         int i;
1683         build_scops_1 (bb, &tmp_scops, loop);
1684
1685         /* Scan the code dominated by this loop.  This means all bbs, that are
1686            are dominated by a bb in this loop, but are not part of this loop.
1687            
1688            The easiest case:
1689              - The loop exit destination is dominated by the exit sources.  
1690          
1691            TODO: We miss here the more complex cases:
1692                   - The exit destinations are dominated by another bb inside the
1693                     loop.
1694                   - The loop dominates bbs, that are not exit destinations.  */
1695         for (i = 0; VEC_iterate (edge, exits, i, e); i++)
1696           if (e->src->loop_father == loop
1697               && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
1698             {
1699               /* Pass loop_outer to recognize e->dest as loop header in
1700                  build_scops_1.  */
1701               if (e->dest->loop_father->header == e->dest)
1702                 build_scops_1 (e->dest, &tmp_scops,
1703                                loop_outer (e->dest->loop_father));
1704               else
1705                 build_scops_1 (e->dest, &tmp_scops, e->dest->loop_father);
1706             }
1707
1708         result.next = NULL; 
1709         result.last = NULL;
1710         result.difficult = true;
1711         result.exits = false;
1712         move_sd_regions (&tmp_scops, scops);
1713         VEC_free (edge, heap, exits);
1714         break;
1715       }
1716     case GBB_COND_HEADER:
1717       {
1718         VEC (sd_region, heap) *tmp_scops = VEC_alloc (sd_region, heap, 3);
1719         struct scopdet_info sinfo;
1720         VEC (basic_block, heap) *dominated;
1721         int i;
1722         basic_block dom_bb;
1723         basic_block last_bb = NULL;
1724         edge e;
1725         result.exits = false;
1726  
1727         /* First check the successors of BB, and check if it is possible to join
1728            the different branches.  */
1729         for (i = 0; VEC_iterate (edge, bb->succs, i, e); i++)
1730           {
1731             /* Ignore loop exits.  They will be handled after the loop body.  */
1732             if (is_loop_exit (loop, e->dest))
1733               {
1734                 result.exits = true;
1735                 continue;
1736               }
1737
1738             /* Do not follow edges that lead to the end of the
1739                conditions block.  For example, in
1740
1741                |   0
1742                |  /|\
1743                | 1 2 |
1744                | | | |
1745                | 3 4 |
1746                |  \|/
1747                |   6
1748
1749                the edge from 0 => 6.  Only check if all paths lead to
1750                the same node 6.  */
1751
1752             if (!single_pred_p (e->dest))
1753               {
1754                 /* Check, if edge leads directly to the end of this
1755                    condition.  */
1756                 if (!last_bb)
1757                   {
1758                     last_bb = e->dest;
1759                   }
1760
1761                 if (e->dest != last_bb)
1762                   result.difficult = true;
1763
1764                 continue;
1765               }
1766
1767             if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
1768               {
1769                 result.difficult = true;
1770                 continue;
1771               }
1772
1773             sinfo = build_scops_1 (e->dest, &tmp_scops, loop);
1774
1775             result.exits |= sinfo.exits;
1776             result.last = sinfo.last;
1777             result.difficult |= sinfo.difficult; 
1778
1779             /* Checks, if all branches end at the same point. 
1780                If that is true, the condition stays joinable.
1781                Have a look at the example above.  */
1782             if (sinfo.last && single_succ_p (sinfo.last))
1783               {
1784                 basic_block next_tmp = single_succ (sinfo.last);
1785                   
1786                 if (!last_bb)
1787                     last_bb = next_tmp;
1788
1789                 if (next_tmp != last_bb)
1790                   result.difficult = true;
1791               }
1792             else
1793               result.difficult = true;
1794           }
1795
1796         /* If the condition is joinable.  */
1797         if (!result.exits && !result.difficult)
1798           {
1799             /* Only return a next pointer if we dominate this pointer.
1800                Otherwise it will be handled by the bb dominating it.  */ 
1801             if (dominated_by_p (CDI_DOMINATORS, last_bb, bb) && last_bb != bb)
1802               result.next = last_bb;
1803             else
1804               result.next = NULL; 
1805
1806             VEC_free (sd_region, heap, tmp_scops);
1807             break;
1808           }
1809
1810         /* Scan remaining bbs dominated by BB.  */
1811         dominated = get_dominated_by (CDI_DOMINATORS, bb);
1812
1813         for (i = 0; VEC_iterate (basic_block, dominated, i, dom_bb); i++)
1814           {
1815             /* Ignore loop exits: they will be handled after the loop body.  */
1816             if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
1817                 < loop_depth (loop))
1818               {
1819                 result.exits = true;
1820                 continue;
1821               }
1822
1823             /* Ignore the bbs processed above.  */
1824             if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
1825               continue;
1826
1827             if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
1828               sinfo = build_scops_1 (dom_bb, &tmp_scops, loop_outer (loop));
1829             else
1830               sinfo = build_scops_1 (dom_bb, &tmp_scops, loop);
1831                                            
1832                                      
1833             result.exits |= sinfo.exits; 
1834             result.difficult = true;
1835             result.last = NULL;
1836           }
1837
1838         VEC_free (basic_block, heap, dominated);
1839
1840         result.next = NULL; 
1841         move_sd_regions (&tmp_scops, scops);
1842
1843         break;
1844       }
1845
1846     default:
1847       gcc_unreachable ();
1848     }
1849
1850   return result;
1851 }
1852
1853 /* Creates the SCoPs and writes entry and exit points for every SCoP.  */
1854
1855 static struct scopdet_info 
1856 build_scops_1 (basic_block current, VEC (sd_region, heap) **scops, loop_p loop)
1857 {
1858   bool in_scop = false;
1859   sd_region open_scop;
1860   struct scopdet_info sinfo;
1861
1862   /* Initialize result.  */ 
1863   struct scopdet_info result;
1864   result.exits = false;
1865   result.difficult = false;
1866   result.next = NULL;
1867   result.last = NULL;
1868   open_scop.entry = NULL;
1869   open_scop.exit = NULL;
1870   sinfo.last = NULL;
1871
1872   /* Loop over the dominance tree.  If we meet a difficult bb, close
1873      the current SCoP.  Loop and condition header start a new layer,
1874      and can only be added if all bbs in deeper layers are simple.  */
1875   while (current != NULL)
1876     {
1877       sinfo = scopdet_basic_block_info (current, scops, get_bb_type (current,
1878                                                                      loop));
1879
1880       if (!in_scop && !(sinfo.exits || sinfo.difficult))
1881         {
1882           open_scop.entry = current;
1883           open_scop.exit = NULL;
1884           in_scop = true;
1885         }
1886       else if (in_scop && (sinfo.exits || sinfo.difficult))
1887         {
1888           open_scop.exit = current;
1889           VEC_safe_push (sd_region, heap, *scops, &open_scop); 
1890           in_scop = false;
1891         }
1892
1893       result.difficult |= sinfo.difficult;
1894       result.exits |= sinfo.exits;
1895
1896       current = sinfo.next;
1897     }
1898
1899   /* Try to close open_scop, if we are still in an open SCoP.  */
1900   if (in_scop)
1901     {
1902       int i;
1903       edge e;
1904
1905         for (i = 0; VEC_iterate (edge, sinfo.last->succs, i, e); i++)
1906           if (dominated_by_p (CDI_POST_DOMINATORS, sinfo.last, e->dest))
1907             open_scop.exit = e->dest;
1908
1909         if (!open_scop.exit && open_scop.entry != sinfo.last)
1910           open_scop.exit = sinfo.last;
1911
1912         if (open_scop.exit)
1913           VEC_safe_push (sd_region, heap, *scops, &open_scop);
1914       
1915     }
1916
1917   result.last = sinfo.last;
1918   return result;
1919 }
1920
1921 /* Checks if a bb is contained in REGION.  */
1922
1923 static bool
1924 bb_in_sd_region (basic_block bb, sd_region *region)
1925 {
1926   return dominated_by_p (CDI_DOMINATORS, bb, region->entry)
1927          && !(dominated_by_p (CDI_DOMINATORS, bb, region->exit)
1928               && !dominated_by_p (CDI_DOMINATORS, region->entry,
1929                                   region->exit));
1930 }
1931
1932 /* Returns the single entry edge of REGION, if it does not exits NULL.  */
1933
1934 static edge
1935 find_single_entry_edge (sd_region *region)
1936 {
1937   edge e;
1938   edge_iterator ei;
1939   edge entry = NULL;
1940
1941   FOR_EACH_EDGE (e, ei, region->entry->preds)
1942     if (!bb_in_sd_region (e->src, region))
1943       {
1944         if (entry)
1945           {
1946             entry = NULL;
1947             break;
1948           }
1949
1950         else
1951           entry = e;
1952       }
1953
1954   return entry;
1955 }
1956
1957 /* Returns the single exit edge of REGION, if it does not exits NULL.  */
1958
1959 static edge
1960 find_single_exit_edge (sd_region *region)
1961 {
1962   edge e;
1963   edge_iterator ei;
1964   edge exit = NULL;
1965
1966   FOR_EACH_EDGE (e, ei, region->exit->preds)
1967     if (bb_in_sd_region (e->src, region))
1968       {
1969         if (exit)
1970           {
1971             exit = NULL;
1972             break;
1973           }
1974
1975         else
1976           exit = e;
1977       }
1978
1979   return exit;
1980 }
1981
1982 /* Create a single entry edge for REGION.  */
1983
1984 static void
1985 create_single_entry_edge (sd_region *region)
1986 {
1987   if (find_single_entry_edge (region))
1988     return;
1989
1990   /* There are multiple predecessors for bb_3 
1991
1992   |  1  2
1993   |  | /
1994   |  |/
1995   |  3  <- entry
1996   |  |\
1997   |  | |
1998   |  4 ^
1999   |  | |
2000   |  |/
2001   |  5
2002
2003   There are two edges (1->3, 2->3), that point from outside into the region,
2004   and another one (5->3), a loop latch, lead to bb_3.
2005
2006   We split bb_3.
2007   
2008   |  1  2
2009   |  | /
2010   |  |/
2011   |3.0
2012   |  |\     (3.0 -> 3.1) = single entry edge
2013   |3.1 |        <- entry
2014   |  | |
2015   |  | |
2016   |  4 ^ 
2017   |  | |
2018   |  |/
2019   |  5
2020
2021   If the loop is part of the SCoP, we have to redirect the loop latches.
2022
2023   |  1  2
2024   |  | /
2025   |  |/
2026   |3.0
2027   |  |      (3.0 -> 3.1) = entry edge
2028   |3.1          <- entry
2029   |  |\
2030   |  | |
2031   |  4 ^
2032   |  | |
2033   |  |/
2034   |  5  */
2035
2036   if (region->entry->loop_father->header != region->entry
2037       || dominated_by_p (CDI_DOMINATORS,
2038                          loop_latch_edge (region->entry->loop_father)->src,
2039                          region->exit))
2040     {
2041       edge forwarder = split_block_after_labels (region->entry);
2042       region->entry = forwarder->dest;
2043     }
2044   else
2045     /* This case is never executed, as the loop headers seem always to have a
2046        single edge pointing from outside into the loop.  */
2047     gcc_unreachable ();
2048       
2049 #ifdef ENABLE_CHECKING
2050   gcc_assert (find_single_entry_edge (region));
2051 #endif
2052 }
2053
2054 /* Check if the sd_region, mentioned in EDGE, has no exit bb.  */
2055
2056 static bool
2057 sd_region_without_exit (edge e)
2058 {
2059   sd_region *r = (sd_region *) e->aux;
2060
2061   if (r)
2062     return r->exit == NULL;
2063   else
2064     return false;
2065 }
2066
2067 /* Create a single exit edge for REGION.  */
2068
2069 static void
2070 create_single_exit_edge (sd_region *region)
2071 {
2072   edge e;
2073   edge_iterator ei;
2074   edge forwarder = NULL;
2075   basic_block exit;
2076   
2077   if (find_single_exit_edge (region))
2078     return;
2079
2080   /* We create a forwarder bb (5) for all edges leaving this region
2081      (3->5, 4->5).  All other edges leading to the same bb, are moved
2082      to a new bb (6).  If these edges where part of another region (2->5)
2083      we update the region->exit pointer, of this region.
2084
2085      To identify which edge belongs to which region we depend on the e->aux
2086      pointer in every edge.  It points to the region of the edge or to NULL,
2087      if the edge is not part of any region.
2088
2089      1 2 3 4    1->5 no region,                 2->5 region->exit = 5,
2090       \| |/     3->5 region->exit = NULL,       4->5 region->exit = NULL
2091         5       <- exit
2092
2093      changes to
2094
2095      1 2 3 4    1->6 no region,                         2->6 region->exit = 6,
2096      | | \/     3->5 no region,                         4->5 no region, 
2097      | |  5
2098       \| /      5->6 region->exit = 6
2099         6 
2100
2101      Now there is only a single exit edge (5->6).  */
2102   exit = region->exit;
2103   region->exit = NULL;
2104   forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
2105   
2106   /* Unmark the edges, that are no longer exit edges.  */
2107   FOR_EACH_EDGE (e, ei, forwarder->src->preds)
2108     if (e->aux)
2109       e->aux = NULL;
2110
2111   /* Mark the new exit edge.  */ 
2112   single_succ_edge (forwarder->src)->aux = region;
2113
2114   /* Update the exit bb of all regions, where exit edges lead to
2115      forwarder->dest.  */
2116   FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
2117     if (e->aux)
2118       ((sd_region *) e->aux)->exit = forwarder->dest;
2119
2120 #ifdef ENABLE_CHECKING
2121   gcc_assert (find_single_exit_edge (region));
2122 #endif
2123 }
2124
2125 /* Unmark the exit edges of all REGIONS.  
2126    See comment in "create_single_exit_edge". */
2127
2128 static void
2129 unmark_exit_edges (VEC (sd_region, heap) *regions)
2130 {
2131   int i;
2132   sd_region *s;
2133   edge e;
2134   edge_iterator ei;
2135
2136   for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
2137     FOR_EACH_EDGE (e, ei, s->exit->preds)
2138       e->aux = NULL;
2139 }
2140
2141
2142 /* Mark the exit edges of all REGIONS.  
2143    See comment in "create_single_exit_edge". */
2144
2145 static void
2146 mark_exit_edges (VEC (sd_region, heap) *regions)
2147 {
2148   int i;
2149   sd_region *s;
2150   edge e;
2151   edge_iterator ei;
2152
2153   for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
2154     FOR_EACH_EDGE (e, ei, s->exit->preds)
2155       if (bb_in_sd_region (e->src, s))
2156         e->aux = s;
2157 }
2158
2159 /* Free and compute again all the dominators information.  */
2160
2161 static inline void
2162 recompute_all_dominators (void)
2163 {
2164   mark_irreducible_loops ();
2165   free_dominance_info (CDI_DOMINATORS);
2166   free_dominance_info (CDI_POST_DOMINATORS);
2167   calculate_dominance_info (CDI_DOMINATORS);
2168   calculate_dominance_info (CDI_POST_DOMINATORS);
2169 }
2170
2171 /* Verifies properties that GRAPHITE should maintain during translation.  */
2172
2173 static inline void
2174 graphite_verify (void)
2175 {
2176 #ifdef ENABLE_CHECKING
2177   verify_loop_structure ();
2178   verify_dominators (CDI_DOMINATORS);
2179   verify_dominators (CDI_POST_DOMINATORS);
2180   verify_ssa (false);
2181 #endif
2182 }
2183
2184 /* Create for all scop regions a single entry and a single exit edge.  */
2185
2186 static void
2187 create_sese_edges (VEC (sd_region, heap) *regions)
2188 {
2189   int i;
2190   sd_region *s;
2191
2192   for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
2193     create_single_entry_edge (s);
2194
2195   mark_exit_edges (regions);
2196
2197   for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
2198     create_single_exit_edge (s);
2199
2200   unmark_exit_edges (regions);
2201
2202   fix_loop_structure (NULL);
2203
2204 #ifdef ENABLE_CHECKING
2205   verify_loop_structure ();
2206   verify_dominators (CDI_DOMINATORS);
2207   verify_ssa (false);
2208 #endif
2209 }
2210
2211 /* Create graphite SCoPs from an array of scop detection regions.  */
2212
2213 static void
2214 build_graphite_scops (VEC (sd_region, heap) *scop_regions)
2215 {
2216   int i;
2217   sd_region *s;
2218
2219   for (i = 0; VEC_iterate (sd_region, scop_regions, i, s); i++)
2220     {
2221       edge entry = find_single_entry_edge (s); 
2222       edge exit = find_single_exit_edge (s);
2223       scop_p scop = new_scop (entry, exit);
2224       VEC_safe_push (scop_p, heap, current_scops, scop);
2225
2226       /* Are there overlapping SCoPs?  */
2227 #ifdef ENABLE_CHECKING
2228         {
2229           int j;
2230           sd_region *s2;
2231
2232           for (j = 0; VEC_iterate (sd_region, scop_regions, j, s2); j++)
2233             if (s != s2)
2234               gcc_assert (!bb_in_sd_region (s->entry, s2));
2235         }
2236 #endif
2237     }
2238 }
2239
2240 /* Find static control parts.  */
2241
2242 static void
2243 build_scops (void)
2244 {
2245   struct loop *loop = current_loops->tree_root;
2246   VEC (sd_region, heap) *tmp_scops = VEC_alloc (sd_region, heap, 3);
2247
2248   build_scops_1 (single_succ (ENTRY_BLOCK_PTR), &tmp_scops, loop);
2249   create_sese_edges (tmp_scops);
2250   build_graphite_scops (tmp_scops);
2251   VEC_free (sd_region, heap, tmp_scops);
2252 }
2253
2254 /* Gather the basic blocks belonging to the SCOP.  */
2255
2256 static void
2257 build_scop_bbs (scop_p scop)
2258 {
2259   basic_block *stack = XNEWVEC (basic_block, n_basic_blocks + 1);
2260   sbitmap visited = sbitmap_alloc (last_basic_block);
2261   int sp = 0;
2262
2263   sbitmap_zero (visited);
2264   stack[sp++] = SCOP_ENTRY (scop);
2265
2266   while (sp)
2267     {
2268       basic_block bb = stack[--sp];
2269       int depth = loop_depth (bb->loop_father);
2270       int num = bb->loop_father->num;
2271       edge_iterator ei;
2272       edge e;
2273
2274       /* Scop's exit is not in the scop.  Exclude also bbs, which are
2275          dominated by the SCoP exit.  These are e.g. loop latches.  */
2276       if (TEST_BIT (visited, bb->index)
2277           || dominated_by_p (CDI_DOMINATORS, bb, SCOP_EXIT (scop))
2278           /* Every block in the scop is dominated by scop's entry.  */
2279           || !dominated_by_p (CDI_DOMINATORS, bb, SCOP_ENTRY (scop)))
2280         continue;
2281
2282       new_graphite_bb (scop, bb);
2283       SET_BIT (visited, bb->index);
2284
2285       /* First push the blocks that have to be processed last.  Note
2286          that this means that the order in which the code is organized
2287          below is important: do not reorder the following code.  */
2288       FOR_EACH_EDGE (e, ei, bb->succs)
2289         if (! TEST_BIT (visited, e->dest->index)
2290             && (int) loop_depth (e->dest->loop_father) < depth)
2291           stack[sp++] = e->dest;
2292
2293       FOR_EACH_EDGE (e, ei, bb->succs)
2294         if (! TEST_BIT (visited, e->dest->index)
2295             && (int) loop_depth (e->dest->loop_father) == depth
2296             && e->dest->loop_father->num != num)
2297           stack[sp++] = e->dest;
2298
2299       FOR_EACH_EDGE (e, ei, bb->succs)
2300         if (! TEST_BIT (visited, e->dest->index)
2301             && (int) loop_depth (e->dest->loop_father) == depth
2302             && e->dest->loop_father->num == num
2303             && EDGE_COUNT (e->dest->preds) > 1)
2304           stack[sp++] = e->dest;
2305
2306       FOR_EACH_EDGE (e, ei, bb->succs)
2307         if (! TEST_BIT (visited, e->dest->index)
2308             && (int) loop_depth (e->dest->loop_father) == depth
2309             && e->dest->loop_father->num == num
2310             && EDGE_COUNT (e->dest->preds) == 1)
2311           stack[sp++] = e->dest;
2312
2313       FOR_EACH_EDGE (e, ei, bb->succs)
2314         if (! TEST_BIT (visited, e->dest->index)
2315             && (int) loop_depth (e->dest->loop_father) > depth)
2316           stack[sp++] = e->dest;
2317     }
2318
2319   free (stack);
2320   sbitmap_free (visited);
2321 }
2322
2323 /* Returns the number of reduction phi nodes in LOOP.  */
2324
2325 static int
2326 nb_reductions_in_loop (loop_p loop)
2327 {
2328   int res = 0;
2329   gimple_stmt_iterator gsi;
2330
2331   for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
2332     {
2333       gimple phi = gsi_stmt (gsi);
2334       tree scev;
2335       affine_iv iv;
2336
2337       if (!is_gimple_reg (PHI_RESULT (phi)))
2338         continue;
2339
2340       scev = analyze_scalar_evolution (loop, PHI_RESULT (phi));
2341       scev = instantiate_parameters (loop, scev);
2342       if (!simple_iv (loop, phi, PHI_RESULT (phi), &iv, true))
2343         res++;
2344     }
2345
2346   return res;
2347 }
2348
2349 /* A LOOP is in normal form when it contains only one scalar phi node
2350    that defines the main induction variable of the loop, only one
2351    increment of the IV, and only one exit condition. */
2352
2353 static tree
2354 graphite_loop_normal_form (loop_p loop)
2355 {
2356   struct tree_niter_desc niter;
2357   tree nit;
2358   gimple_seq stmts;
2359   edge exit = single_dom_exit (loop);
2360
2361   gcc_assert (number_of_iterations_exit (loop, exit, &niter, false));
2362   nit = force_gimple_operand (unshare_expr (niter.niter), &stmts, true,
2363                               NULL_TREE);
2364   if (stmts)
2365     gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2366
2367   /* One IV per loop.  */
2368   if (nb_reductions_in_loop (loop) > 0)
2369     return NULL_TREE;
2370
2371   return canonicalize_loop_ivs (loop, NULL, nit);
2372 }
2373
2374 /* Record LOOP as occuring in SCOP.  Returns true when the operation
2375    was successful.  */
2376
2377 static bool
2378 scop_record_loop (scop_p scop, loop_p loop)
2379 {
2380   tree induction_var;
2381   name_tree oldiv;
2382
2383   if (bitmap_bit_p (SCOP_LOOPS (scop), loop->num))
2384     return true;
2385
2386   bitmap_set_bit (SCOP_LOOPS (scop), loop->num);
2387   VEC_safe_push (loop_p, heap, SCOP_LOOP_NEST (scop), loop);
2388
2389   induction_var = graphite_loop_normal_form (loop);
2390   if (!induction_var)
2391     return false;
2392
2393   oldiv = XNEW (struct name_tree);
2394   oldiv->t = induction_var;
2395   oldiv->name = get_name (SSA_NAME_VAR (oldiv->t));
2396   oldiv->loop = loop;
2397   VEC_safe_push (name_tree, heap, SCOP_OLDIVS (scop), oldiv);
2398   return true;
2399 }
2400
2401 /* Build the loop nests contained in SCOP.  Returns true when the
2402    operation was successful.  */
2403
2404 static bool
2405 build_scop_loop_nests (scop_p scop)
2406 {
2407   unsigned i;
2408   basic_block bb;
2409   struct loop *loop0, *loop1;
2410
2411   FOR_EACH_BB (bb)
2412     if (bb_in_scop_p (bb, scop))
2413       {
2414         struct loop *loop = bb->loop_father;
2415
2416         /* Only add loops if they are completely contained in the SCoP.  */
2417         if (loop->header == bb
2418             && bb_in_scop_p (loop->latch, scop))
2419           {
2420             if (!scop_record_loop (scop, loop))
2421               return false;
2422           }
2423       }
2424
2425   /* Make sure that the loops in the SCOP_LOOP_NEST are ordered.  It
2426      can be the case that an inner loop is inserted before an outer
2427      loop.  To avoid this, semi-sort once.  */
2428   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop0); i++)
2429     {
2430       if (VEC_length (loop_p, SCOP_LOOP_NEST (scop)) == i + 1)
2431         break;
2432
2433       loop1 = VEC_index (loop_p, SCOP_LOOP_NEST (scop), i + 1);
2434       if (loop0->num > loop1->num)
2435         {
2436           VEC_replace (loop_p, SCOP_LOOP_NEST (scop), i, loop1);
2437           VEC_replace (loop_p, SCOP_LOOP_NEST (scop), i + 1, loop0);
2438         }
2439     }
2440
2441   return true;
2442 }
2443
2444 /* Build dynamic schedules for all the BBs. */
2445
2446 static void
2447 build_scop_dynamic_schedules (scop_p scop)
2448 {
2449   int i, dim, loop_num, row, col;
2450   graphite_bb_p gb;
2451
2452   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
2453     {
2454       loop_num = GBB_BB (gb)->loop_father->num;
2455
2456       if (loop_num != 0)
2457         {
2458           dim = nb_loops_around_gb (gb);
2459           GBB_DYNAMIC_SCHEDULE (gb) = cloog_matrix_alloc (dim, dim);
2460
2461           for (row = 0; row < GBB_DYNAMIC_SCHEDULE (gb)->NbRows; row++)
2462             for (col = 0; col < GBB_DYNAMIC_SCHEDULE (gb)->NbColumns; col++)
2463               if (row == col)
2464                 value_set_si (GBB_DYNAMIC_SCHEDULE (gb)->p[row][col], 1);
2465               else
2466                 value_set_si (GBB_DYNAMIC_SCHEDULE (gb)->p[row][col], 0);
2467         }
2468       else
2469         GBB_DYNAMIC_SCHEDULE (gb) = NULL;
2470     }
2471 }
2472
2473 /* Returns the number of loops that are identical at the beginning of
2474    the vectors A and B.  */
2475
2476 static int
2477 compare_prefix_loops (VEC (loop_p, heap) *a, VEC (loop_p, heap) *b)
2478 {
2479   int i;
2480   loop_p ea;
2481   int lb;
2482
2483   if (!a || !b)
2484     return 0;
2485
2486   lb = VEC_length (loop_p, b);
2487
2488   for (i = 0; VEC_iterate (loop_p, a, i, ea); i++)
2489     if (i >= lb
2490         || ea != VEC_index (loop_p, b, i))
2491       return i;
2492
2493   return 0;
2494 }
2495
2496 /* Build for BB the static schedule.
2497
2498    The STATIC_SCHEDULE is defined like this:
2499
2500    A
2501    for (i: ...)
2502      {
2503        for (j: ...)
2504          {
2505            B
2506            C 
2507          }
2508
2509        for (k: ...)
2510          {
2511            D
2512            E 
2513          }
2514      }
2515    F
2516
2517    Static schedules for A to F:
2518
2519      DEPTH
2520      0 1 2 
2521    A 0
2522    B 1 0 0
2523    C 1 0 1
2524    D 1 1 0
2525    E 1 1 1 
2526    F 2
2527 */
2528
2529 static void
2530 build_scop_canonical_schedules (scop_p scop)
2531 {
2532   int i;
2533   graphite_bb_p gb;
2534   int nb_loops = scop_nb_loops (scop);
2535   lambda_vector static_schedule = lambda_vector_new (nb_loops + 1);
2536   VEC (loop_p, heap) *loops_previous = NULL;
2537
2538   /* We have to start schedules at 0 on the first component and
2539      because we cannot compare_prefix_loops against a previous loop,
2540      prefix will be equal to zero, and that index will be
2541      incremented before copying.  */
2542   static_schedule[0] = -1;
2543
2544   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
2545     {
2546       int prefix = compare_prefix_loops (loops_previous, GBB_LOOPS (gb));
2547       int nb = gbb_nb_loops (gb);
2548
2549       loops_previous = GBB_LOOPS (gb);
2550       memset (&(static_schedule[prefix + 1]), 0, sizeof (int) * (nb_loops - prefix));
2551       ++static_schedule[prefix];
2552       GBB_STATIC_SCHEDULE (gb) = lambda_vector_new (nb + 1);
2553       lambda_vector_copy (static_schedule, 
2554                           GBB_STATIC_SCHEDULE (gb), nb + 1);
2555     }
2556 }
2557
2558 /* Build the LOOPS vector for all bbs in SCOP.  */
2559
2560 static void
2561 build_bb_loops (scop_p scop)
2562 {
2563   graphite_bb_p gb;
2564   int i;
2565
2566   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
2567     {
2568       loop_p loop;
2569       int depth; 
2570
2571       depth = nb_loops_around_gb (gb) - 1; 
2572
2573       GBB_LOOPS (gb) = VEC_alloc (loop_p, heap, 3);
2574       VEC_safe_grow_cleared (loop_p, heap, GBB_LOOPS (gb), depth + 1);
2575
2576       loop = GBB_BB (gb)->loop_father;  
2577
2578       while (scop_contains_loop (scop, loop))
2579         {
2580           VEC_replace (loop_p, GBB_LOOPS (gb), depth, loop);
2581           loop = loop_outer (loop);
2582           depth--;
2583         }
2584     }
2585 }
2586
2587 /* Get the index for parameter VAR in SCOP.  */
2588
2589 static int
2590 param_index (tree var, scop_p scop)
2591 {
2592   int i;
2593   name_tree p;
2594   name_tree nvar;
2595
2596   gcc_assert (TREE_CODE (var) == SSA_NAME);
2597
2598   for (i = 0; VEC_iterate (name_tree, SCOP_PARAMS (scop), i, p); i++)
2599     if (p->t == var)
2600       return i;
2601
2602   gcc_assert (SCOP_ADD_PARAMS (scop));
2603
2604   nvar = XNEW (struct name_tree);
2605   nvar->t = var;
2606   nvar->name = NULL;
2607   VEC_safe_push (name_tree, heap, SCOP_PARAMS (scop), nvar);
2608   return VEC_length (name_tree, SCOP_PARAMS (scop)) - 1;
2609 }
2610
2611 /* Scan EXPR and translate it to an inequality vector INEQ that will
2612    be added, or subtracted, in the constraint domain matrix C at row
2613    R.  K is the number of columns for loop iterators in C. */ 
2614
2615 static void
2616 scan_tree_for_params (scop_p s, tree e, CloogMatrix *c, int r, Value k,
2617                       bool subtract)
2618 {
2619   int cst_col, param_col;
2620
2621   if (e == chrec_dont_know)
2622     return;
2623
2624   switch (TREE_CODE (e))
2625     {
2626     case POLYNOMIAL_CHREC:
2627       {
2628         tree left = CHREC_LEFT (e);
2629         tree right = CHREC_RIGHT (e);
2630         int var = CHREC_VARIABLE (e);
2631
2632         if (TREE_CODE (right) != INTEGER_CST)
2633           return;
2634
2635         if (c)
2636           {
2637             int loop_col = scop_gimple_loop_depth (s, get_loop (var)) + 1;
2638
2639             if (subtract)
2640               value_sub_int (c->p[r][loop_col], c->p[r][loop_col],
2641                              int_cst_value (right));
2642             else
2643               value_add_int (c->p[r][loop_col], c->p[r][loop_col],
2644                              int_cst_value (right));
2645           }
2646
2647         switch (TREE_CODE (left))
2648           {
2649           case POLYNOMIAL_CHREC:
2650             scan_tree_for_params (s, left, c, r, k, subtract);
2651             return;
2652
2653           case INTEGER_CST:
2654             /* Constant part.  */
2655             if (c)
2656               {
2657                 int v = int_cst_value (left);
2658                 cst_col = c->NbColumns - 1;
2659
2660                 if (v < 0)
2661                   {
2662                     v = -v;
2663                     subtract = subtract ? false : true;
2664                   }
2665
2666                 if (subtract)
2667                   value_sub_int (c->p[r][cst_col], c->p[r][cst_col], v);
2668                 else
2669                   value_add_int (c->p[r][cst_col], c->p[r][cst_col], v);
2670               }
2671             return;
2672
2673           default:
2674             scan_tree_for_params (s, left, c, r, k, subtract);
2675             return;
2676           }
2677       }
2678       break;
2679
2680     case MULT_EXPR:
2681       if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
2682         {
2683           if (c)
2684             {
2685               Value val;
2686               gcc_assert (host_integerp (TREE_OPERAND (e, 1), 0));
2687               value_init (val);
2688               value_set_si (val, int_cst_value (TREE_OPERAND (e, 1)));
2689               value_multiply (k, k, val);
2690               value_clear (val);
2691             }
2692           scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
2693         }
2694       else
2695         {
2696           if (c)
2697             {
2698               Value val;
2699               gcc_assert (host_integerp (TREE_OPERAND (e, 0), 0));
2700               value_init (val);
2701               value_set_si (val, int_cst_value (TREE_OPERAND (e, 0)));
2702               value_multiply (k, k, val);
2703               value_clear (val);
2704             }
2705           scan_tree_for_params (s, TREE_OPERAND (e, 1), c, r, k, subtract);
2706         }
2707       break;
2708
2709     case PLUS_EXPR:
2710     case POINTER_PLUS_EXPR:
2711       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
2712       scan_tree_for_params (s, TREE_OPERAND (e, 1), c, r, k, subtract);
2713       break;
2714
2715     case MINUS_EXPR:
2716       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
2717       scan_tree_for_params (s, TREE_OPERAND (e, 1), c, r, k, !subtract);
2718       break;
2719
2720     case NEGATE_EXPR:
2721       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, !subtract);
2722       break;
2723
2724     case SSA_NAME:
2725       param_col = param_index (e, s);
2726
2727       if (c)
2728         {
2729           param_col += c->NbColumns - scop_nb_params (s) - 1;
2730
2731           if (subtract)
2732             value_subtract (c->p[r][param_col], c->p[r][param_col], k);
2733           else
2734             value_addto (c->p[r][param_col], c->p[r][param_col], k);
2735         }
2736       break;
2737
2738     case INTEGER_CST:
2739       if (c)
2740         {
2741           int v = int_cst_value (e);
2742           cst_col = c->NbColumns - 1;
2743
2744           if (v < 0)
2745           {
2746             v = -v;
2747             subtract = subtract ? false : true;
2748           }
2749                 
2750           if (subtract)
2751             value_sub_int (c->p[r][cst_col], c->p[r][cst_col], v); 
2752           else
2753             value_add_int (c->p[r][cst_col], c->p[r][cst_col], v);
2754         }
2755       break;
2756
2757     CASE_CONVERT:
2758     case NON_LVALUE_EXPR:
2759       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, r, k, subtract);
2760       break;
2761
2762     default:
2763       gcc_unreachable ();
2764       break;
2765     }
2766 }
2767
2768 /* Data structure for idx_record_params.  */
2769
2770 struct irp_data
2771 {
2772   struct loop *loop;
2773   scop_p scop;
2774 };
2775
2776 /* For a data reference with an ARRAY_REF as its BASE, record the
2777    parameters occurring in IDX.  DTA is passed in as complementary
2778    information, and is used by the automatic walker function.  This
2779    function is a callback for for_each_index.  */
2780
2781 static bool
2782 idx_record_params (tree base, tree *idx, void *dta)
2783 {
2784   struct irp_data *data = (struct irp_data *) dta;
2785
2786   if (TREE_CODE (base) != ARRAY_REF)
2787     return true;
2788
2789   if (TREE_CODE (*idx) == SSA_NAME)
2790     {
2791       tree scev;
2792       scop_p scop = data->scop;
2793       struct loop *loop = data->loop;
2794       Value one;
2795
2796       scev = analyze_scalar_evolution (loop, *idx);
2797       scev = instantiate_scev (block_before_scop (scop), loop, scev);
2798
2799       value_init (one);
2800       value_set_si (one, 1);
2801       scan_tree_for_params (scop, scev, NULL, 0, one, false);
2802       value_clear (one);
2803     }
2804
2805   return true;
2806 }
2807
2808 /* Find parameters with respect to SCOP in BB. We are looking in memory
2809    access functions, conditions and loop bounds.  */
2810
2811 static void
2812 find_params_in_bb (scop_p scop, graphite_bb_p gb)
2813 {
2814   int i;
2815   data_reference_p dr;
2816   gimple stmt;
2817   loop_p father = GBB_BB (gb)->loop_father;
2818
2819   for (i = 0; VEC_iterate (data_reference_p, GBB_DATA_REFS (gb), i, dr); i++)
2820     {
2821       struct irp_data irp;
2822
2823       irp.loop = father;
2824       irp.scop = scop;
2825       for_each_index (&dr->ref, idx_record_params, &irp);
2826     }
2827
2828   /* Find parameters in conditional statements.  */ 
2829   for (i = 0; VEC_iterate (gimple, GBB_CONDITIONS (gb), i, stmt); i++)
2830     {
2831       Value one;
2832       loop_p loop = father;
2833
2834       tree lhs, rhs;
2835
2836       lhs = gimple_cond_lhs (stmt);
2837       lhs = analyze_scalar_evolution (loop, lhs);
2838       lhs = instantiate_scev (block_before_scop (scop), loop, lhs);
2839
2840       rhs = gimple_cond_rhs (stmt);
2841       rhs = analyze_scalar_evolution (loop, rhs);
2842       rhs = instantiate_scev (block_before_scop (scop), loop, rhs);
2843
2844       value_init (one);
2845       scan_tree_for_params (scop, lhs, NULL, 0, one, false);
2846       value_set_si (one, 1);
2847       scan_tree_for_params (scop, rhs, NULL, 0, one, false);
2848       value_clear (one);
2849     }
2850 }
2851
2852 /* Saves in NV the name of variable P->T.  */
2853
2854 static void
2855 save_var_name (char **nv, int i, name_tree p)
2856 {
2857   const char *name = get_name (SSA_NAME_VAR (p->t));
2858
2859   if (name)
2860     {
2861       int len = strlen (name) + 16;
2862       nv[i] = XNEWVEC (char, len);
2863       snprintf (nv[i], len, "%s_%d", name, SSA_NAME_VERSION (p->t));
2864     }
2865   else
2866     {
2867       nv[i] = XNEWVEC (char, 16);
2868       snprintf (nv[i], 2 + 16, "T_%d", SSA_NAME_VERSION (p->t));
2869     }
2870
2871   p->name = nv[i];
2872 }
2873
2874 /* Return the maximal loop depth in SCOP.  */
2875
2876 static int
2877 scop_max_loop_depth (scop_p scop)
2878 {
2879   int i;
2880   graphite_bb_p gbb;
2881   int max_nb_loops = 0;
2882
2883   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++) 
2884     {    
2885       int nb_loops = gbb_nb_loops (gbb);
2886       if (max_nb_loops < nb_loops)
2887         max_nb_loops = nb_loops;
2888     }    
2889
2890   return max_nb_loops;
2891 }
2892
2893 /* Initialize Cloog's parameter names from the names used in GIMPLE.
2894    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
2895    from 0 to scop_nb_loops (scop).  */
2896
2897 static void
2898 initialize_cloog_names (scop_p scop)
2899 {
2900   int i, nb_params = VEC_length (name_tree, SCOP_PARAMS (scop));
2901   char **params = XNEWVEC (char *, nb_params);
2902   int nb_iterators = scop_max_loop_depth (scop);
2903   int nb_scattering= cloog_program_nb_scattdims (SCOP_PROG (scop));
2904   char **iterators = XNEWVEC (char *, nb_iterators * 2);
2905   char **scattering = XNEWVEC (char *, nb_scattering);
2906   name_tree p;
2907
2908   for (i = 0; VEC_iterate (name_tree, SCOP_PARAMS (scop), i, p); i++)
2909     save_var_name (params, i, p);
2910
2911   cloog_names_set_nb_parameters (cloog_program_names (SCOP_PROG (scop)),
2912                                  nb_params);
2913   cloog_names_set_parameters (cloog_program_names (SCOP_PROG (scop)),
2914                               params);
2915
2916   for (i = 0; i < nb_iterators; i++)
2917     {
2918       int len = 18 + 16;
2919       iterators[i] = XNEWVEC (char, len);
2920       snprintf (iterators[i], len, "graphite_iterator_%d", i);
2921     }
2922
2923   cloog_names_set_nb_iterators (cloog_program_names (SCOP_PROG (scop)),
2924                                 nb_iterators);
2925   cloog_names_set_iterators (cloog_program_names (SCOP_PROG (scop)),
2926                              iterators);
2927
2928   for (i = 0; i < nb_scattering; i++)
2929     {
2930       int len = 2 + 16;
2931       scattering[i] = XNEWVEC (char, len);
2932       snprintf (scattering[i], len, "s_%d", i);
2933     }
2934
2935   cloog_names_set_nb_scattering (cloog_program_names (SCOP_PROG (scop)),
2936                                  nb_scattering);
2937   cloog_names_set_scattering (cloog_program_names (SCOP_PROG (scop)),
2938                               scattering);
2939 }
2940
2941 /* Record the parameters used in the SCOP.  A variable is a parameter
2942    in a scop if it does not vary during the execution of that scop.  */
2943
2944 static void
2945 find_scop_parameters (scop_p scop)
2946 {
2947   graphite_bb_p gb;
2948   unsigned i;
2949   struct loop *loop;
2950   Value one;
2951
2952   value_init (one);
2953   value_set_si (one, 1);
2954
2955   /* Find the parameters used in the loop bounds.  */
2956   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop); i++)
2957     {
2958       tree nb_iters = number_of_latch_executions (loop);
2959
2960       if (!chrec_contains_symbols (nb_iters))
2961         continue;
2962
2963       nb_iters = analyze_scalar_evolution (loop, nb_iters);
2964       nb_iters = instantiate_scev (block_before_scop (scop), loop, nb_iters);
2965       scan_tree_for_params (scop, nb_iters, NULL, 0, one, false);
2966     }
2967
2968   value_clear (one);
2969
2970   /* Find the parameters used in data accesses.  */
2971   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
2972     find_params_in_bb (scop, gb);
2973
2974   SCOP_ADD_PARAMS (scop) = false;
2975 }
2976
2977 /* Build the context constraints for SCOP: constraints and relations
2978    on parameters.  */
2979
2980 static void
2981 build_scop_context (scop_p scop)
2982 {
2983   int nb_params = scop_nb_params (scop);
2984   CloogMatrix *matrix = cloog_matrix_alloc (1, nb_params + 2);
2985
2986   /* Insert '0 >= 0' in the context matrix, as it is not allowed to be
2987      empty. */
2988  
2989   value_set_si (matrix->p[0][0], 1);
2990
2991   value_set_si (matrix->p[0][nb_params + 1], 0);
2992
2993   cloog_program_set_context (SCOP_PROG (scop),
2994                              cloog_domain_matrix2domain (matrix));
2995   cloog_matrix_free (matrix);
2996 }
2997
2998 /* Returns a graphite_bb from BB.  */
2999
3000 static inline graphite_bb_p
3001 gbb_from_bb (basic_block bb)
3002 {
3003   return (graphite_bb_p) bb->aux;
3004 }
3005
3006 /* Builds the constraint matrix for LOOP in SCOP.  NB_OUTER_LOOPS is the
3007    number of loops surrounding LOOP in SCOP.  OUTER_CSTR gives the
3008    constraints matrix for the surrounding loops.  */
3009
3010 static void
3011 build_loop_iteration_domains (scop_p scop, struct loop *loop,
3012                               CloogMatrix *outer_cstr, int nb_outer_loops)
3013 {
3014   int i, j, row;
3015   CloogMatrix *cstr;
3016   graphite_bb_p gb;
3017
3018   int nb_rows = outer_cstr->NbRows + 1;
3019   int nb_cols = outer_cstr->NbColumns + 1;
3020
3021   /* Last column of CSTR is the column of constants.  */
3022   int cst_col = nb_cols - 1;
3023
3024   /* The column for the current loop is just after the columns of
3025      other outer loops.  */
3026   int loop_col = nb_outer_loops + 1;
3027
3028   tree nb_iters = number_of_latch_executions (loop);
3029
3030   /* When the number of iterations is a constant or a parameter, we
3031      add a constraint for the upper bound of the loop.  So add a row
3032      to the constraint matrix before allocating it.  */
3033   if (TREE_CODE (nb_iters) == INTEGER_CST
3034       || !chrec_contains_undetermined (nb_iters))
3035     nb_rows++;
3036
3037   cstr = cloog_matrix_alloc (nb_rows, nb_cols);
3038
3039   /* Copy the outer constraints.  */
3040   for (i = 0; i < outer_cstr->NbRows; i++)
3041     {
3042       /* Copy the eq/ineq and loops columns.  */
3043       for (j = 0; j < loop_col; j++)
3044         value_assign (cstr->p[i][j], outer_cstr->p[i][j]);
3045
3046       /* Leave an empty column in CSTR for the current loop, and then
3047          copy the parameter columns.  */
3048       for (j = loop_col; j < outer_cstr->NbColumns; j++)
3049         value_assign (cstr->p[i][j + 1], outer_cstr->p[i][j]);
3050     }
3051
3052   /* 0 <= loop_i */
3053   row = outer_cstr->NbRows;
3054   value_set_si (cstr->p[row][0], 1);
3055   value_set_si (cstr->p[row][loop_col], 1);
3056
3057   /* loop_i <= nb_iters */
3058   if (TREE_CODE (nb_iters) == INTEGER_CST)
3059     {
3060       row++;
3061       value_set_si (cstr->p[row][0], 1);
3062       value_set_si (cstr->p[row][loop_col], -1);
3063
3064       value_set_si (cstr->p[row][cst_col],
3065                     int_cst_value (nb_iters));
3066     }
3067   else if (!chrec_contains_undetermined (nb_iters))
3068     {
3069       /* Otherwise nb_iters contains parameters: scan the nb_iters
3070          expression and build its matrix representation.  */
3071       Value one;
3072
3073       row++;
3074       value_set_si (cstr->p[row][0], 1);
3075       value_set_si (cstr->p[row][loop_col], -1);
3076
3077       nb_iters = analyze_scalar_evolution (loop, nb_iters);
3078       nb_iters = instantiate_scev (block_before_scop (scop), loop, nb_iters);
3079
3080       value_init (one);
3081       value_set_si (one, 1);
3082       scan_tree_for_params (scop, nb_iters, cstr, row, one, false);
3083       value_clear (one);
3084     }
3085   else
3086     gcc_unreachable ();
3087
3088   if (loop->inner && loop_in_scop_p (loop->inner, scop))
3089     build_loop_iteration_domains (scop, loop->inner, cstr, nb_outer_loops + 1);
3090
3091   /* Only go to the next loops, if we are not at the outermost layer.  These
3092      have to be handled seperately, as we can be sure, that the chain at this
3093      layer will be connected.  */
3094   if (nb_outer_loops != 0 && loop->next && loop_in_scop_p (loop->next, scop))
3095     build_loop_iteration_domains (scop, loop->next, outer_cstr, nb_outer_loops);
3096
3097   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
3098     if (gbb_loop (gb) == loop)
3099       GBB_DOMAIN (gb) = cloog_matrix_copy (cstr);
3100
3101   cloog_matrix_free (cstr);
3102 }
3103
3104 /* Add conditions to the domain of GB.  */
3105
3106 static void
3107 add_conditions_to_domain (graphite_bb_p gb)
3108 {
3109   unsigned int i,j;
3110   gimple stmt;
3111   VEC (gimple, heap) *conditions = GBB_CONDITIONS (gb);
3112   CloogMatrix *domain = GBB_DOMAIN (gb);
3113   scop_p scop = GBB_SCOP (gb);
3114
3115   unsigned nb_rows;
3116   unsigned nb_cols;
3117   unsigned nb_new_rows = 0;
3118   unsigned row;
3119
3120   if (VEC_empty (gimple, conditions))
3121     return;
3122
3123   if (domain)
3124     {
3125       nb_rows = domain->NbRows;
3126       nb_cols = domain->NbColumns;
3127     }
3128   else  
3129     {
3130       nb_rows = 0;
3131       nb_cols = nb_loops_around_gb (gb) + scop_nb_params (scop) + 2;
3132     }
3133
3134   /* Count number of necessary new rows to add the conditions to the
3135      domain.  */
3136   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
3137     {
3138       switch (gimple_code (stmt))
3139         {
3140         case GIMPLE_COND:
3141           {
3142             enum tree_code code = gimple_cond_code (stmt);
3143
3144             switch (code)
3145               {
3146               case NE_EXPR:
3147               case EQ_EXPR:
3148                 /* NE and EQ statements are not supported right know. */
3149                 gcc_unreachable ();
3150                 break;
3151               case LT_EXPR:
3152               case GT_EXPR:
3153               case LE_EXPR:
3154               case GE_EXPR:
3155                 nb_new_rows++;
3156                 break;
3157               default:
3158                 gcc_unreachable ();
3159                 break;
3160               }
3161           break;
3162           }
3163         case SWITCH_EXPR:
3164           /* Switch statements are not supported right know.  */
3165           gcc_unreachable ();
3166           break;
3167
3168         default:
3169           gcc_unreachable ();
3170           break;
3171         }
3172     }
3173
3174
3175   /* Enlarge the matrix.  */ 
3176   { 
3177     CloogMatrix *new_domain;
3178     new_domain = cloog_matrix_alloc (nb_rows + nb_new_rows, nb_cols);
3179
3180     if (domain)
3181       {
3182         for (i = 0; i < nb_rows; i++)
3183           for (j = 0; j < nb_cols; j++)
3184             value_assign (new_domain->p[i][j], domain->p[i][j]);
3185
3186         cloog_matrix_free (domain);
3187       }
3188
3189     domain = new_domain;
3190     GBB_DOMAIN (gb) = new_domain;
3191   }
3192
3193   /* Add the conditions to the new enlarged domain matrix.  */
3194   row = nb_rows;
3195   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
3196     {
3197       switch (gimple_code (stmt))
3198         {
3199         case GIMPLE_COND:
3200           {
3201             Value one;
3202             enum tree_code code;
3203             tree left;
3204             tree right;
3205             loop_p loop = GBB_BB (gb)->loop_father;
3206
3207             left = gimple_cond_lhs (stmt);
3208             right = gimple_cond_rhs (stmt);
3209
3210             left = analyze_scalar_evolution (loop, left);
3211             right = analyze_scalar_evolution (loop, right);
3212
3213             left = instantiate_scev (block_before_scop (scop), loop, left);
3214             right = instantiate_scev (block_before_scop (scop), loop, right);
3215
3216             code = gimple_cond_code (stmt);
3217
3218             /* The conditions for ELSE-branches are inverted.  */
3219             if (VEC_index (gimple, gb->condition_cases, i) == NULL)
3220               code = invert_tree_comparison (code, false);
3221
3222             switch (code)
3223               {
3224               case NE_EXPR:
3225                 /* NE statements are not supported right know. */
3226                 gcc_unreachable ();
3227                 break;
3228               case EQ_EXPR:
3229                 value_set_si (domain->p[row][0], 1);
3230                 value_init (one);
3231                 value_set_si (one, 1);
3232                 scan_tree_for_params (scop, left, domain, row, one, true);
3233                 value_set_si (one, 1);
3234                 scan_tree_for_params (scop, right, domain, row, one, false);
3235                 row++;
3236                 value_set_si (domain->p[row][0], 1);
3237                 value_set_si (one, 1);
3238                 scan_tree_for_params (scop, left, domain, row, one, false);
3239                 value_set_si (one, 1);
3240                 scan_tree_for_params (scop, right, domain, row, one, true);
3241                 value_clear (one);
3242                 row++;
3243                 break;
3244               case LT_EXPR:
3245                 value_set_si (domain->p[row][0], 1);
3246                 value_init (one);
3247                 value_set_si (one, 1);
3248                 scan_tree_for_params (scop, left, domain, row, one, true);
3249                 value_set_si (one, 1);
3250                 scan_tree_for_params (scop, right, domain, row, one, false);
3251                 value_sub_int (domain->p[row][nb_cols - 1],
3252                     domain->p[row][nb_cols - 1], 1); 
3253                 value_clear (one);
3254                 row++;
3255                 break;
3256               case GT_EXPR:
3257                 value_set_si (domain->p[row][0], 1);
3258                 value_init (one);
3259                 value_set_si (one, 1);
3260                 scan_tree_for_params (scop, left, domain, row, one, false);
3261                 value_set_si (one, 1);
3262                 scan_tree_for_params (scop, right, domain, row, one, true);
3263                 value_sub_int (domain->p[row][nb_cols - 1],
3264                     domain->p[row][nb_cols - 1], 1);
3265                 value_clear (one);
3266                 row++;
3267                 break;
3268               case LE_EXPR:
3269                 value_set_si (domain->p[row][0], 1);
3270                 value_init (one);
3271                 value_set_si (one, 1);
3272                 scan_tree_for_params (scop, left, domain, row, one, true);
3273                 value_set_si (one, 1);
3274                 scan_tree_for_params (scop, right, domain, row, one, false);
3275                 value_clear (one);
3276                 row++;
3277                 break;
3278               case GE_EXPR:
3279                 value_set_si (domain->p[row][0], 1);
3280                 value_init (one);
3281                 value_set_si (one, 1);
3282                 scan_tree_for_params (scop, left, domain, row, one, false);
3283                 value_set_si (one, 1);
3284                 scan_tree_for_params (scop, right, domain, row, one, true);
3285                 value_clear (one);
3286                 row++;
3287                 break;
3288               default:
3289                 gcc_unreachable ();
3290                 break;
3291               }
3292             break;
3293           }
3294         case GIMPLE_SWITCH:
3295           /* Switch statements are not supported right know.  */
3296           gcc_unreachable ();
3297           break;
3298
3299         default:
3300           gcc_unreachable ();
3301           break;
3302         }
3303     }
3304 }
3305
3306 /* Returns true when PHI defines an induction variable in the loop
3307    containing the PHI node.  */
3308
3309 static bool
3310 phi_node_is_iv (gimple phi)
3311 {
3312   loop_p loop = gimple_bb (phi)->loop_father;
3313   tree scev = analyze_scalar_evolution (loop, gimple_phi_result (phi));
3314
3315   return tree_contains_chrecs (scev, NULL);
3316 }
3317
3318 /* Returns true when BB contains scalar phi nodes that are not an
3319    induction variable of a loop.  */
3320
3321 static bool
3322 bb_contains_non_iv_scalar_phi_nodes (basic_block bb)
3323 {
3324   gimple phi = NULL;
3325   gimple_stmt_iterator si;
3326
3327   for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
3328     if (is_gimple_reg (gimple_phi_result (gsi_stmt (si))))
3329       {
3330         /* Store the unique scalar PHI node: at this point, loops
3331            should be in cannonical form, so we expect to see at most
3332            one scalar phi node in the loop header.  */
3333         if (phi
3334             || bb != bb->loop_father->header)
3335           return true;
3336
3337         phi = gsi_stmt (si);
3338       }
3339
3340   if (!phi
3341       || phi_node_is_iv (phi))
3342     return false;
3343
3344   return true;
3345 }
3346
3347 /* Helper recursive function.  Record in CONDITIONS and CASES all
3348    conditions from 'if's and 'switch'es occurring in BB from SCOP.
3349
3350    Returns false when the conditions contain scalar computations that
3351    depend on the condition, i.e. when there are scalar phi nodes on
3352    the junction after the condition.  Only the computations occurring
3353    on memory can be handled in the polyhedral model: operations that
3354    define scalar evolutions in conditions, that can potentially be
3355    used to index memory, can't be handled by the polyhedral model.  */
3356
3357 static bool
3358 build_scop_conditions_1 (VEC (gimple, heap) **conditions,
3359                          VEC (gimple, heap) **cases, basic_block bb,
3360                          scop_p scop)
3361 {
3362   bool res = true;
3363   int i, j;
3364   graphite_bb_p gbb;
3365   gimple_stmt_iterator gsi;
3366   basic_block bb_child, bb_iter;
3367   VEC (basic_block, heap) *dom;
3368   
3369   /* Make sure we are in the SCoP.  */
3370   if (!bb_in_scop_p (bb, scop))
3371     return true;
3372
3373   if (bb_contains_non_iv_scalar_phi_nodes (bb))
3374     return false;
3375
3376   gbb = gbb_from_bb (bb);
3377   if (gbb)
3378     {
3379       GBB_CONDITIONS (gbb) = VEC_copy (gimple, heap, *conditions);
3380       GBB_CONDITION_CASES (gbb) = VEC_copy (gimple, heap, *cases);
3381     }
3382
3383   dom = get_dominated_by (CDI_DOMINATORS, bb);
3384
3385   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3386     {
3387       gimple stmt = gsi_stmt (gsi);
3388       VEC (edge, gc) *edges;
3389       edge e;
3390
3391       switch (gimple_code (stmt))
3392         {
3393         case GIMPLE_COND:
3394           edges = bb->succs;
3395           for (i = 0; VEC_iterate (edge, edges, i, e); i++)
3396             if ((dominated_by_p (CDI_DOMINATORS, e->dest, bb))
3397                 && VEC_length (edge, e->dest->preds) == 1)
3398               {
3399                 /* Remove the scanned block from the dominator successors.  */
3400                 for (j = 0; VEC_iterate (basic_block, dom, j, bb_iter); j++)
3401                   if (bb_iter == e->dest)
3402                     {
3403                       VEC_unordered_remove (basic_block, dom, j);
3404                       break;
3405                     }
3406
3407                 /* Recursively scan the then or else part.  */
3408                 if (e->flags & EDGE_TRUE_VALUE)
3409                   VEC_safe_push (gimple, heap, *cases, stmt);
3410                 else 
3411                   {
3412                     gcc_assert (e->flags & EDGE_FALSE_VALUE);
3413                     VEC_safe_push (gimple, heap, *cases, NULL);
3414                   }
3415
3416                 VEC_safe_push (gimple, heap, *conditions, stmt);
3417                 if (!build_scop_conditions_1 (conditions, cases, e->dest, scop))
3418                   {
3419                     res = false;
3420                     goto done;
3421                   }
3422                 VEC_pop (gimple, *conditions);
3423                 VEC_pop (gimple, *cases);
3424               }
3425           break;
3426
3427         case GIMPLE_SWITCH:
3428           {
3429             unsigned i;
3430             gimple_stmt_iterator gsi_search_gimple_label;
3431
3432             for (i = 0; i < gimple_switch_num_labels (stmt); ++i)
3433               {
3434                 basic_block bb_iter;
3435                 size_t k;
3436                 size_t n_cases = VEC_length (gimple, *conditions);
3437                 unsigned n = gimple_switch_num_labels (stmt);
3438
3439                 bb_child = label_to_block
3440                   (CASE_LABEL (gimple_switch_label (stmt, i)));
3441
3442                 for (k = 0; k < n; k++)
3443                   if (i != k
3444                       && label_to_block 
3445                       (CASE_LABEL (gimple_switch_label (stmt, k))) == bb_child)
3446                     break;
3447
3448                 /* Switches with multiple case values for the same
3449                    block are not handled.  */
3450                 if (k != n
3451                     /* Switch cases with more than one predecessor are
3452                        not handled.  */
3453                     || VEC_length (edge, bb_child->preds) != 1)
3454                   {
3455                     res = false;
3456                     goto done;
3457                   }
3458
3459                 /* Recursively scan the corresponding 'case' block.  */
3460                 for (gsi_search_gimple_label = gsi_start_bb (bb_child);
3461                      !gsi_end_p (gsi_search_gimple_label);
3462                      gsi_next (&gsi_search_gimple_label))
3463                   {
3464                     gimple label = gsi_stmt (gsi_search_gimple_label);
3465
3466                     if (gimple_code (label) == GIMPLE_LABEL)
3467                       {
3468                         tree t = gimple_label_label (label);
3469
3470                         gcc_assert (t == gimple_switch_label (stmt, i));
3471                         VEC_replace (gimple, *cases, n_cases, label);
3472                         break;
3473                       }
3474                   }
3475
3476                 if (!build_scop_conditions_1 (conditions, cases, bb_child, scop))
3477                   {
3478                     res = false;
3479                     goto done;
3480                   }
3481
3482                 /* Remove the scanned block from the dominator successors.  */
3483                 for (j = 0; VEC_iterate (basic_block, dom, j, bb_iter); j++)
3484                   if (bb_iter == bb_child)
3485                     {
3486                       VEC_unordered_remove (basic_block, dom, j);
3487                       break;
3488                     }
3489               }
3490
3491             VEC_pop (gimple, *conditions);
3492             VEC_pop (gimple, *cases);
3493             break;
3494           }
3495
3496         default:
3497           break;
3498       }
3499   }
3500
3501   /* Scan all immediate dominated successors.  */
3502   for (i = 0; VEC_iterate (basic_block, dom, i, bb_child); i++)
3503     if (!build_scop_conditions_1 (conditions, cases, bb_child, scop))
3504       {
3505         res = false;
3506         goto done;
3507       }
3508
3509  done:
3510   VEC_free (basic_block, heap, dom);
3511   return res;
3512 }
3513
3514 /* Record all conditions from SCOP.
3515
3516    Returns false when the conditions contain scalar computations that
3517    depend on the condition, i.e. when there are scalar phi nodes on
3518    the junction after the condition.  Only the computations occurring
3519    on memory can be handled in the polyhedral model: operations that
3520    define scalar evolutions in conditions, that can potentially be
3521    used to index memory, can't be handled by the polyhedral model.  */
3522
3523 static bool
3524 build_scop_conditions (scop_p scop)
3525 {
3526   bool res;
3527   VEC (gimple, heap) *conditions = NULL;
3528   VEC (gimple, heap) *cases = NULL;
3529
3530   res = build_scop_conditions_1 (&conditions, &cases, SCOP_ENTRY (scop), scop);
3531
3532   VEC_free (gimple, heap, conditions);
3533   VEC_free (gimple, heap, cases);
3534   return res;
3535 }
3536
3537 /* Traverses all the GBBs of the SCOP and add their constraints to the
3538    iteration domains.  */
3539
3540 static void
3541 add_conditions_to_constraints (scop_p scop)
3542 {
3543   int i;
3544   graphite_bb_p gbb;
3545
3546   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++)
3547     add_conditions_to_domain (gbb);
3548 }
3549
3550 /* Build the current domain matrix: the loops belonging to the current
3551    SCOP, and that vary for the execution of the current basic block.
3552    Returns false if there is no loop in SCOP.  */
3553
3554 static bool
3555 build_scop_iteration_domain (scop_p scop)
3556 {
3557   struct loop *loop;
3558   CloogMatrix *outer_cstr;
3559   int i;
3560
3561   /* Build cloog loop for all loops, that are in the uppermost loop layer of
3562      this SCoP.  */
3563   for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, loop); i++)
3564     if (!loop_in_scop_p (loop_outer (loop), scop))
3565       {
3566         /* The outermost constraints is a matrix that has:
3567            -first column: eq/ineq boolean
3568            -last column: a constant
3569            -scop_nb_params columns for the parameters used in the scop.  */
3570         outer_cstr = cloog_matrix_alloc (0, scop_nb_params (scop) + 2);
3571         build_loop_iteration_domains (scop, loop, outer_cstr, 0);
3572         cloog_matrix_free (outer_cstr);
3573       }
3574
3575   return (i != 0);
3576 }
3577
3578 /* Initializes an equation CY of the access matrix using the
3579    information for a subscript from AF, relatively to the loop
3580    indexes from LOOP_NEST and parameter indexes from PARAMS.  NDIM is
3581    the dimension of the array access, i.e. the number of
3582    subscripts.  Returns true when the operation succeeds.  */
3583
3584 static bool
3585 build_access_matrix_with_af (tree af, lambda_vector cy,
3586                              scop_p scop, int ndim)
3587 {
3588   int param_col;
3589
3590   switch (TREE_CODE (af))
3591     {
3592     case POLYNOMIAL_CHREC:
3593       {
3594         struct loop *outer_loop;
3595         tree left = CHREC_LEFT (af);
3596         tree right = CHREC_RIGHT (af);
3597         int var;
3598
3599         if (TREE_CODE (right) != INTEGER_CST)
3600           return false;
3601
3602         outer_loop = get_loop (CHREC_VARIABLE (af));
3603         var = nb_loops_around_loop_in_scop (outer_loop, scop);
3604         cy[var] = int_cst_value (right);
3605
3606         switch (TREE_CODE (left))
3607           {
3608           case POLYNOMIAL_CHREC:
3609             return build_access_matrix_with_af (left, cy, scop, ndim);
3610
3611           case INTEGER_CST:
3612             cy[ndim - 1] = int_cst_value (left);
3613             return true;
3614
3615           default:
3616             return build_access_matrix_with_af (left, cy, scop, ndim);
3617           }
3618       }
3619
3620     case PLUS_EXPR:
3621       build_access_matrix_with_af (TREE_OPERAND (af, 0), cy, scop, ndim);
3622       build_access_matrix_with_af (TREE_OPERAND (af, 1), cy, scop, ndim);
3623       return true;
3624       
3625     case MINUS_EXPR:
3626       build_access_matrix_with_af (TREE_OPERAND (af, 0), cy, scop, ndim);
3627       build_access_matrix_with_af (TREE_OPERAND (af, 1), cy, scop, ndim);
3628       return true;
3629
3630     case INTEGER_CST:
3631       cy[ndim - 1] = int_cst_value (af);
3632       return true;
3633
3634     case SSA_NAME:
3635       param_col = param_index (af, scop);      
3636       cy [ndim - scop_nb_params (scop) + param_col - 1] = 1; 
3637       return true;
3638
3639     default:
3640       /* FIXME: access_fn can have parameters.  */
3641       return false;
3642     }
3643 }
3644
3645 /* Initialize the access matrix in the data reference REF with respect
3646    to the loop nesting LOOP_NEST.  Return true when the operation
3647    succeeded.  */
3648
3649 static bool
3650 build_access_matrix (data_reference_p ref, graphite_bb_p gb)
3651 {
3652   int i, ndim = DR_NUM_DIMENSIONS (ref);
3653   struct access_matrix *am = GGC_NEW (struct access_matrix);
3654
3655   AM_MATRIX (am) = VEC_alloc (lambda_vector, gc, ndim);
3656   DR_SCOP (ref) = GBB_SCOP (gb);
3657
3658   for (i = 0; i < ndim; i++)
3659     {
3660       lambda_vector v = lambda_vector_new (ref_nb_loops (ref));
3661       scop_p scop = GBB_SCOP (gb);
3662       tree af = DR_ACCESS_FN (ref, i);
3663
3664       if (!build_access_matrix_with_af (af, v, scop, ref_nb_loops (ref)))
3665         return false;
3666
3667       VEC_quick_push (lambda_vector, AM_MATRIX (am), v);
3668     }
3669
3670   DR_ACCESS_MATRIX (ref) = am;
3671   return true;
3672 }
3673
3674 /* Build the access matrices for the data references in the SCOP.  */
3675
3676 static void
3677 build_scop_data_accesses (scop_p scop)
3678 {
3679   int i;
3680   graphite_bb_p gb;
3681
3682   /* FIXME: Construction of access matrix is disabled until some
3683      pass, like the data dependence analysis, is using it.  */
3684   return;
3685
3686   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
3687     {
3688       int j;
3689       data_reference_p dr;
3690
3691       /* Construct the access matrix for each data ref, with respect to
3692          the loop nest of the current BB in the considered SCOP.  */
3693       for (j = 0;
3694            VEC_iterate (data_reference_p, GBB_DATA_REFS (gb), j, dr);
3695            j++)
3696         {
3697           bool res = build_access_matrix (dr, gb);
3698
3699           /* FIXME: At this point the DRs should always have an affine
3700              form.  For the moment this fails as build_access_matrix
3701              does not build matrices with parameters.  */
3702           gcc_assert (res);
3703         }
3704     }
3705 }
3706
3707 /* Returns the tree variable from the name NAME that was given in
3708    Cloog representation.  All the parameters are stored in PARAMS, and
3709    all the loop induction variables are stored in IVSTACK.
3710
3711    FIXME: This is a hack, and Cloog should be fixed to not work with
3712    variable names represented as "char *string", but with void
3713    pointers that could be casted back to a tree.  The only problem in
3714    doing that is that Cloog's pretty printer still assumes that
3715    variable names are char *strings.  The solution would be to have a
3716    function pointer for pretty-printing that can be redirected to be
3717    print_generic_stmt in our case, or fprintf by default.
3718    ???  Too ugly to live.  */
3719
3720 static tree
3721 clast_name_to_gcc (const char *name, VEC (name_tree, heap) *params, 
3722                    loop_iv_stack ivstack)
3723 {
3724   int i;
3725   name_tree t;
3726   tree iv;
3727
3728   if (params)
3729     for (i = 0; VEC_iterate (name_tree, params, i, t); i++)
3730       if (!strcmp (name, t->name))
3731         return t->t;
3732
3733   iv = loop_iv_stack_get_iv_from_name (ivstack, name);
3734   if (iv)
3735     return iv;
3736
3737   gcc_unreachable ();
3738 }
3739
3740 /* Returns the maximal precision type for expressions E1 and E2.  */
3741
3742 static inline tree
3743 max_precision_type (tree e1, tree e2)
3744 {
3745   tree type1 = TREE_TYPE (e1);
3746   tree type2 = TREE_TYPE (e2);
3747   return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
3748 }
3749
3750 static tree
3751 clast_to_gcc_expression (tree, struct clast_expr *, VEC (name_tree, heap) *,
3752                          loop_iv_stack);
3753
3754 /* Converts a Cloog reduction expression R with reduction operation OP
3755    to a GCC expression tree of type TYPE.  PARAMS is a vector of
3756    parameters of the scop, and IVSTACK contains the stack of induction
3757    variables.  */
3758
3759 static tree
3760 clast_to_gcc_expression_red (tree type, enum tree_code op,
3761                              struct clast_reduction *r,
3762                              VEC (name_tree, heap) *params,
3763                              loop_iv_stack ivstack)
3764 {
3765   int i;
3766   tree res = clast_to_gcc_expression (type, r->elts[0], params, ivstack);
3767
3768   for (i = 1; i < r->n; i++)
3769     {
3770       tree t = clast_to_gcc_expression (type, r->elts[i], params, ivstack);
3771       res = fold_build2 (op, type, res, t);
3772     }
3773   return res;
3774 }
3775
3776 /* Converts a Cloog AST expression E back to a GCC expression tree of
3777    type TYPE.  PARAMS is a vector of parameters of the scop, and
3778    IVSTACK contains the stack of induction variables.  */
3779
3780 static tree
3781 clast_to_gcc_expression (tree type, struct clast_expr *e,
3782                          VEC (name_tree, heap) *params,
3783                          loop_iv_stack ivstack)
3784 {
3785   switch (e->type)
3786     {
3787     case expr_term:
3788       {
3789         struct clast_term *t = (struct clast_term *) e;
3790
3791         if (t->var)
3792           {
3793             if (value_one_p (t->val))
3794               {
3795                 tree name = clast_name_to_gcc (t->var, params, ivstack);
3796                 return fold_convert (type, name);
3797               }
3798
3799             else if (value_mone_p (t->val))
3800               {
3801                 tree name = clast_name_to_gcc (t->var, params, ivstack);
3802                 name = fold_convert (type, name);
3803                 return fold_build1 (NEGATE_EXPR, type, name);
3804               }
3805             else
3806               {
3807                 tree name = clast_name_to_gcc (t->var, params, ivstack);
3808                 tree cst = gmp_cst_to_tree (type, t->val);
3809                 name = fold_convert (type, name);
3810                 return fold_build2 (MULT_EXPR, type, cst, name);
3811               }
3812           }
3813         else
3814           return gmp_cst_to_tree (type, t->val);
3815       }
3816
3817     case expr_red:
3818       {
3819         struct clast_reduction *r = (struct clast_reduction *) e;
3820
3821         switch (r->type)
3822           {
3823           case clast_red_sum:
3824             return clast_to_gcc_expression_red (type, PLUS_EXPR, r, params, ivstack);
3825
3826           case clast_red_min:
3827             return clast_to_gcc_expression_red (type, MIN_EXPR, r, params, ivstack);
3828
3829           case clast_red_max:
3830             return clast_to_gcc_expression_red (type, MAX_EXPR, r, params, ivstack);
3831
3832           default:
3833             gcc_unreachable ();
3834           }
3835         break;
3836       }
3837
3838     case expr_bin:
3839       {
3840         struct clast_binary *b = (struct clast_binary *) e;
3841         struct clast_expr *lhs = (struct clast_expr *) b->LHS;
3842         tree tl = clast_to_gcc_expression (type, lhs, params, ivstack);
3843         tree tr = gmp_cst_to_tree (type, b->RHS);
3844
3845         switch (b->type)
3846           {
3847           case clast_bin_fdiv:
3848             return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
3849
3850           case clast_bin_cdiv:
3851             return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
3852
3853           case clast_bin_div:
3854             return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
3855
3856           case clast_bin_mod:
3857             return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
3858
3859           default:
3860             gcc_unreachable ();
3861           }
3862       }
3863
3864     default:
3865       gcc_unreachable ();
3866     }
3867
3868   return NULL_TREE;
3869 }
3870
3871 /* Returns the type for the expression E.  */
3872
3873 static tree
3874 gcc_type_for_clast_expr (struct clast_expr *e,
3875                          VEC (name_tree, heap) *params,
3876                          loop_iv_stack ivstack)
3877 {
3878   switch (e->type)
3879     {
3880     case expr_term:
3881       {
3882         struct clast_term *t = (struct clast_term *) e;
3883
3884         if (t->var)
3885           return TREE_TYPE (clast_name_to_gcc (t->var, params, ivstack));
3886         else
3887           return NULL_TREE;
3888       }
3889
3890     case expr_red:
3891       {
3892         struct clast_reduction *r = (struct clast_reduction *) e;
3893
3894         if (r->n == 1)
3895           return gcc_type_for_clast_expr (r->elts[0], params, ivstack);
3896         else 
3897           {
3898             int i;
3899             for (i = 0; i < r->n; i++)
3900               {
3901                 tree type = gcc_type_for_clast_expr (r->elts[i], params, ivstack);
3902                 if (type)
3903                   return type;
3904               }
3905             return NULL_TREE;
3906           }
3907       }
3908
3909     case expr_bin:
3910       {
3911         struct clast_binary *b = (struct clast_binary *) e;
3912         struct clast_expr *lhs = (struct clast_expr *) b->LHS;
3913         return gcc_type_for_clast_expr (lhs, params, ivstack);
3914       }
3915
3916     default:
3917       gcc_unreachable ();
3918     }
3919
3920   return NULL_TREE;
3921 }
3922
3923 /* Returns the type for the equation CLEQ.  */
3924
3925 static tree
3926 gcc_type_for_clast_eq (struct clast_equation *cleq,
3927                        VEC (name_tree, heap) *params,
3928                        loop_iv_stack ivstack)
3929 {
3930   tree type = gcc_type_for_clast_expr (cleq->LHS, params, ivstack);
3931   if (type)
3932     return type;
3933
3934   return gcc_type_for_clast_expr (cleq->RHS, params, ivstack);
3935 }
3936
3937 /* Translates a clast equation CLEQ to a tree.  */
3938
3939 static tree
3940 graphite_translate_clast_equation (scop_p scop,
3941                                    struct clast_equation *cleq,
3942                                    loop_iv_stack ivstack)
3943 {
3944   enum tree_code comp;
3945   tree type = gcc_type_for_clast_eq (cleq, SCOP_PARAMS (scop), ivstack);
3946   tree lhs = clast_to_gcc_expression (type, cleq->LHS, SCOP_PARAMS (scop), ivstack);
3947   tree rhs = clast_to_gcc_expression (type, cleq->RHS, SCOP_PARAMS (scop), ivstack);
3948
3949   if (cleq->sign == 0)
3950     comp = EQ_EXPR;
3951
3952   else if (cleq->sign > 0)
3953     comp = GE_EXPR;
3954
3955   else
3956     comp = LE_EXPR;
3957
3958   return fold_build2 (comp, type, lhs, rhs);
3959 }
3960
3961 /* Creates the test for the condition in STMT.  */
3962
3963 static tree
3964 graphite_create_guard_cond_expr (scop_p scop, struct clast_guard *stmt, 
3965                                  loop_iv_stack ivstack)
3966 {
3967   tree cond = NULL;
3968   int i;
3969
3970   for (i = 0; i < stmt->n; i++)
3971     {
3972       tree eq = graphite_translate_clast_equation (scop, &stmt->eq[i], ivstack);
3973
3974       if (cond)
3975         cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
3976       else
3977         cond = eq;
3978     }
3979
3980   return cond;
3981 }
3982
3983 /* Creates a new if region corresponding to Cloog's guard.  */
3984
3985 static edge 
3986 graphite_create_new_guard (scop_p scop, edge entry_edge,
3987                            struct clast_guard *stmt, 
3988                            loop_iv_stack ivstack)
3989 {
3990   tree cond_expr = graphite_create_guard_cond_expr (scop, stmt, ivstack);
3991   edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
3992   return exit_edge;
3993 }
3994
3995 /* Walks a CLAST and returns the first statement in the body of a
3996    loop.  */
3997
3998 static struct clast_user_stmt *
3999 clast_get_body_of_loop (struct clast_stmt *stmt)
4000 {
4001   if (!stmt
4002       || CLAST_STMT_IS_A (stmt, stmt_user))
4003     return (struct clast_user_stmt *) stmt;
4004
4005   if (CLAST_STMT_IS_A (stmt, stmt_for))
4006     return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
4007
4008   if (CLAST_STMT_IS_A (stmt, stmt_guard))
4009     return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
4010
4011   if (CLAST_STMT_IS_A (stmt, stmt_block))
4012     return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
4013
4014   gcc_unreachable ();
4015 }
4016
4017 /* Returns the induction variable for the loop that gets translated to
4018    STMT.  */
4019
4020 static tree
4021 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for)
4022 {
4023   struct clast_user_stmt *stmt = clast_get_body_of_loop ((struct clast_stmt *) stmt_for);
4024   const char *cloog_iv = stmt_for->iterator;
4025   CloogStatement *cs = stmt->statement;
4026   graphite_bb_p gbb = (graphite_bb_p) cloog_statement_usr (cs);
4027
4028   return gcc_type_for_cloog_iv (cloog_iv, gbb);
4029 }
4030
4031 /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an induction 
4032    variable for the new LOOP.  New LOOP is attached to CFG starting at
4033    ENTRY_EDGE.  LOOP is inserted into the loop tree and becomes the child
4034    loop of the OUTER_LOOP.  */
4035
4036 static struct loop *
4037 graphite_create_new_loop (scop_p scop, edge entry_edge,
4038                           struct clast_for *stmt, loop_iv_stack ivstack,
4039                           loop_p outer)
4040 {
4041   tree type = gcc_type_for_iv_of_clast_loop (stmt);
4042   VEC (name_tree, heap) *params = SCOP_PARAMS (scop);
4043   tree lb = clast_to_gcc_expression (type, stmt->LB, params, ivstack);
4044   tree ub = clast_to_gcc_expression (type, stmt->UB, params, ivstack);
4045   tree stride = gmp_cst_to_tree (type, stmt->stride);
4046   tree ivvar = create_tmp_var (type, "graphiteIV");
4047   tree iv_before;
4048   loop_p loop = create_empty_loop_on_edge
4049     (entry_edge, lb, stride, ub, ivvar, &iv_before,
4050      outer ? outer : entry_edge->src->loop_father);
4051
4052   add_referenced_var (ivvar);
4053   loop_iv_stack_push_iv (ivstack, iv_before, stmt->iterator);
4054   return loop;
4055 }
4056
4057 /* Rename the SSA_NAMEs used in STMT and that appear in IVSTACK.  */
4058
4059 static void 
4060 rename_variables_in_stmt (gimple stmt, htab_t map)
4061 {
4062   ssa_op_iter iter;
4063   use_operand_p use_p;
4064
4065   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4066     {
4067       tree use = USE_FROM_PTR (use_p);
4068       tree new_name = get_new_name_from_old_name (map, use);
4069
4070       replace_exp (use_p, new_name);
4071     }
4072
4073   update_stmt (stmt);
4074 }
4075
4076 /* Returns true if SSA_NAME is a parameter of SCOP.  */
4077
4078 static bool
4079 is_parameter (scop_p scop, tree ssa_name)
4080 {
4081   int i;
4082   VEC (name_tree, heap) *params = SCOP_PARAMS (scop);
4083   name_tree param;
4084
4085   for (i = 0; VEC_iterate (name_tree, params, i, param); i++)
4086     if (param->t == ssa_name)
4087       return true;
4088
4089   return false;
4090 }
4091
4092 /* Returns true if NAME is an induction variable.  */
4093
4094 static bool
4095 is_iv (tree name)
4096 {
4097   return gimple_code (SSA_NAME_DEF_STMT (name)) == GIMPLE_PHI;
4098 }
4099
4100 static void expand_scalar_variables_stmt (gimple, basic_block, scop_p,
4101                                           htab_t);
4102 static tree
4103 expand_scalar_variables_expr (tree, tree, enum tree_code, tree, basic_block,
4104                               scop_p, htab_t, gimple_stmt_iterator *);
4105
4106 /* Copies at GSI all the scalar computations on which the ssa_name OP0
4107    depends on in the SCOP: these are all the scalar variables used in
4108    the definition of OP0, that are defined outside BB and still in the
4109    SCOP, i.e. not a parameter of the SCOP.  The expression that is
4110    returned contains only induction variables from the generated code:
4111    MAP contains the induction variables renaming mapping, and is used
4112    to translate the names of induction variables.  */
4113
4114 static tree
4115 expand_scalar_variables_ssa_name (tree op0, basic_block bb,
4116                                   scop_p scop, htab_t map, 
4117                                   gimple_stmt_iterator *gsi)
4118 {
4119   tree var0, var1, type;
4120   gimple def_stmt;
4121   enum tree_code subcode;
4122       
4123   if (is_parameter (scop, op0)
4124       || is_iv (op0))
4125     return get_new_name_from_old_name (map, op0);
4126       
4127   def_stmt = SSA_NAME_DEF_STMT (op0);
4128       
4129   if (gimple_bb (def_stmt) == bb)
4130     {
4131       /* If the defining statement is in the basic block already
4132          we do not need to create a new expression for it, we
4133          only need to ensure its operands are expanded.  */
4134       expand_scalar_variables_stmt (def_stmt, bb, scop, map);
4135       return get_new_name_from_old_name (map, op0);
4136     }
4137   else
4138     {
4139       if (gimple_code (def_stmt) != GIMPLE_ASSIGN
4140           || !bb_in_scop_p (gimple_bb (def_stmt), scop))
4141         return get_new_name_from_old_name (map, op0);
4142
4143       var0 = gimple_assign_rhs1 (def_stmt);
4144       subcode = gimple_assign_rhs_code (def_stmt);
4145       var1 = gimple_assign_rhs2 (def_stmt);
4146       type = gimple_expr_type (def_stmt);
4147
4148       return expand_scalar_variables_expr (type, var0, subcode, var1, bb, scop,
4149                                            map, gsi);
4150     }
4151 }
4152
4153 /* Copies at GSI all the scalar computations on which the expression
4154    OP0 CODE OP1 depends on in the SCOP: these are all the scalar
4155    variables used in OP0 and OP1, defined outside BB and still defined
4156    in the SCOP, i.e. not a parameter of the SCOP.  The expression that
4157    is returned contains only induction variables from the generated
4158    code: MAP contains the induction variables renaming mapping, and is
4159    used to translate the names of induction variables.  */
4160
4161 static tree
4162 expand_scalar_variables_expr (tree type, tree op0, enum tree_code code, 
4163                               tree op1, basic_block bb, scop_p scop, 
4164                               htab_t map, gimple_stmt_iterator *gsi)
4165 {
4166   if (TREE_CODE_CLASS (code) == tcc_constant
4167       || TREE_CODE_CLASS (code) == tcc_declaration)
4168     return op0;
4169
4170   /* For data references we have to duplicate also its memory
4171      indexing.  */
4172   if (TREE_CODE_CLASS (code) == tcc_reference)
4173     {
4174       switch (code)
4175         {
4176         case INDIRECT_REF:
4177           {
4178             tree old_name = TREE_OPERAND (op0, 0);
4179             tree expr = expand_scalar_variables_ssa_name
4180               (old_name, bb, scop, map, gsi);
4181             tree new_name = force_gimple_operand_gsi (gsi, expr, true, NULL,
4182                                                       true, GSI_SAME_STMT);
4183
4184             set_symbol_mem_tag (SSA_NAME_VAR (new_name),
4185                                 symbol_mem_tag (SSA_NAME_VAR (old_name)));
4186             return fold_build1 (code, type, new_name);
4187           }
4188
4189         case ARRAY_REF:
4190           {
4191             tree op00 = TREE_OPERAND (op0, 0);
4192             tree op01 = TREE_OPERAND (op0, 1);
4193             tree op02 = TREE_OPERAND (op0, 2);
4194             tree op03 = TREE_OPERAND (op0, 3);
4195             tree base = expand_scalar_variables_expr
4196               (TREE_TYPE (op00), op00, TREE_CODE (op00), NULL, bb, scop,
4197                map, gsi);
4198             tree subscript = expand_scalar_variables_expr
4199               (TREE_TYPE (op01), op01, TREE_CODE (op01), NULL, bb, scop,
4200                map, gsi);
4201
4202             return build4 (ARRAY_REF, type, base, subscript, op02, op03);
4203           }
4204
4205         default:
4206           /* The above cases should catch everything.  */
4207           gcc_unreachable ();
4208         }
4209     }
4210
4211   if (TREE_CODE_CLASS (code) == tcc_unary)
4212     {
4213       tree op0_type = TREE_TYPE (op0);
4214       enum tree_code op0_code = TREE_CODE (op0);
4215       tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
4216                                                     NULL, bb, scop, map, gsi);
4217   
4218       return fold_build1 (code, type, op0_expr);
4219     }
4220
4221   if (TREE_CODE_CLASS (code) == tcc_binary)
4222     {
4223       tree op0_type = TREE_TYPE (op0);
4224       enum tree_code op0_code = TREE_CODE (op0);
4225       tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
4226                                                     NULL, bb, scop, map, gsi);
4227       tree op1_type = TREE_TYPE (op1);
4228       enum tree_code op1_code = TREE_CODE (op1);
4229       tree op1_expr = expand_scalar_variables_expr (op1_type, op1, op1_code,
4230                                                     NULL, bb, scop, map, gsi);
4231
4232       return fold_build2 (code, type, op0_expr, op1_expr);
4233     }
4234
4235   if (code == SSA_NAME)
4236     return expand_scalar_variables_ssa_name (op0, bb, scop, map, gsi);
4237
4238   gcc_unreachable ();
4239   return NULL;
4240 }
4241
4242 /* Copies at the beginning of BB all the scalar computations on which
4243    STMT depends on in the SCOP: these are all the scalar variables used
4244    in STMT, defined outside BB and still defined in the SCOP, i.e. not a
4245    parameter of the SCOP.  The expression that is returned contains
4246    only induction variables from the generated code: MAP contains the
4247    induction variables renaming mapping, and is used to translate the
4248    names of induction variables.  */
4249  
4250 static void
4251 expand_scalar_variables_stmt (gimple stmt, basic_block bb, scop_p scop,
4252                               htab_t map)
4253 {
4254   ssa_op_iter iter;
4255   use_operand_p use_p;
4256   gimple_stmt_iterator gsi = gsi_after_labels (bb);
4257
4258   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4259     {
4260       tree use = USE_FROM_PTR (use_p);
4261       tree type = TREE_TYPE (use);
4262       enum tree_code code = TREE_CODE (use);
4263       tree use_expr = expand_scalar_variables_expr (type, use, code, NULL, bb,
4264                                                     scop, map, &gsi);
4265       if (use_expr != use)
4266         {
4267           tree new_use =
4268             force_gimple_operand_gsi (&gsi, use_expr, true, NULL,
4269                                       true, GSI_NEW_STMT);
4270           replace_exp (use_p, new_use);
4271         }
4272     }
4273
4274   update_stmt (stmt);
4275 }
4276
4277 /* Copies at the beginning of BB all the scalar computations on which
4278    BB depends on in the SCOP: these are all the scalar variables used
4279    in BB, defined outside BB and still defined in the SCOP, i.e. not a
4280    parameter of the SCOP.  The expression that is returned contains
4281    only induction variables from the generated code: MAP contains the
4282    induction variables renaming mapping, and is used to translate the
4283    names of induction variables.  */
4284
4285 static void 
4286 expand_scalar_variables (basic_block bb, scop_p scop, htab_t map)
4287 {
4288   gimple_stmt_iterator gsi;
4289   
4290   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
4291     {
4292       gimple stmt = gsi_stmt (gsi);
4293       expand_scalar_variables_stmt (stmt, bb, scop, map);
4294       gsi_next (&gsi);
4295     }
4296 }
4297
4298 /* Rename all the SSA_NAMEs from block BB according to the MAP.  */
4299
4300 static void 
4301 rename_variables (basic_block bb, htab_t map)
4302 {
4303   gimple_stmt_iterator gsi;
4304   
4305   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4306     rename_variables_in_stmt (gsi_stmt (gsi), map);
4307 }
4308
4309 /* Remove condition from BB.  */
4310
4311 static void
4312 remove_condition (basic_block bb)
4313 {
4314   gimple last = last_stmt (bb);
4315
4316   if (last && gimple_code (last) == GIMPLE_COND)
4317     {
4318       gimple_stmt_iterator gsi = gsi_last_bb (bb);
4319       gsi_remove (&gsi, true);
4320     }
4321 }
4322
4323 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set.  */
4324
4325 static edge
4326 get_true_edge_from_guard_bb (basic_block bb)
4327 {
4328   edge e;
4329   edge_iterator ei;
4330
4331   FOR_EACH_EDGE (e, ei, bb->succs)
4332     if (e->flags & EDGE_TRUE_VALUE) 
4333       return e;
4334
4335   gcc_unreachable ();
4336   return NULL;
4337 }
4338
4339 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared.  */
4340
4341 static edge
4342 get_false_edge_from_guard_bb (basic_block bb)
4343 {
4344   edge e;
4345   edge_iterator ei;
4346
4347   FOR_EACH_EDGE (e, ei, bb->succs)
4348     if (!(e->flags & EDGE_TRUE_VALUE)) 
4349       return e;
4350
4351   gcc_unreachable ();
4352   return NULL;
4353 }
4354
4355 /* Inserts in MAP a tuple (OLD_NAME, NEW_NAME) for the induction
4356    variables of the loops around GBB in SCOP, i.e. GBB_LOOPS.
4357    NEW_NAME is obtained from IVSTACK.  IVSTACK has the same stack
4358    ordering as GBB_LOOPS.  */
4359
4360 static void
4361 build_iv_mapping (loop_iv_stack ivstack, htab_t map, gbb_p gbb, scop_p scop)
4362 {
4363   int i;
4364   name_tree iv;
4365   PTR *slot;
4366
4367   for (i = 0; VEC_iterate (name_tree, SCOP_OLDIVS (scop), i, iv); i++)
4368     {
4369       struct rename_map_elt tmp;
4370
4371       if (!flow_bb_inside_loop_p (iv->loop, GBB_BB (gbb)))
4372         continue;
4373
4374       tmp.old_name = iv->t;
4375       slot = htab_find_slot (map, &tmp, INSERT);
4376
4377       if (!*slot)
4378         {
4379           tree new_name = loop_iv_stack_get_iv (ivstack, 
4380                                                 gbb_loop_index (gbb, iv->loop));
4381           *slot = new_rename_map_elt (iv->t, new_name);
4382         }
4383     }
4384 }
4385
4386 /* Register in MAP the tuple (old_name, new_name).  */
4387
4388 static void
4389 register_old_and_new_names (htab_t map, tree old_name, tree new_name)
4390 {
4391   struct rename_map_elt tmp;
4392   PTR *slot;
4393
4394   tmp.old_name = old_name;
4395   slot = htab_find_slot (map, &tmp, INSERT);
4396
4397   if (!*slot)
4398     *slot = new_rename_map_elt (old_name, new_name);
4399 }
4400
4401 /* Create a duplicate of the basic block BB.  NOTE: This does not
4402    preserve SSA form.  */
4403
4404 static void
4405 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb, htab_t map)
4406 {
4407   gimple_stmt_iterator gsi, gsi_tgt;
4408
4409   gsi_tgt = gsi_start_bb (new_bb);
4410   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4411     {
4412       def_operand_p def_p;
4413       ssa_op_iter op_iter;
4414       int region;
4415       gimple stmt = gsi_stmt (gsi);
4416       gimple copy;
4417
4418       if (gimple_code (stmt) == GIMPLE_LABEL)
4419         continue;
4420
4421       /* Create a new copy of STMT and duplicate STMT's virtual
4422          operands.  */
4423       copy = gimple_copy (stmt);
4424       gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
4425       mark_symbols_for_renaming (copy);
4426
4427       region = lookup_stmt_eh_region (stmt);
4428       if (region >= 0)
4429         add_stmt_to_eh_region (copy, region);
4430       gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
4431
4432       /* Create new names for all the definitions created by COPY and
4433          add replacement mappings for each new name.  */
4434       FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_DEF)
4435         {
4436           tree old_name = DEF_FROM_PTR (def_p);
4437           tree new_name = create_new_def_for (old_name, copy, def_p);
4438           register_old_and_new_names (map, old_name, new_name);
4439         }
4440     }
4441 }
4442
4443 /* Records in SCOP_LIVEOUT_RENAMES the names that are live out of
4444    the SCOP and that appear in the RENAME_MAP.  */
4445
4446 static void
4447 register_scop_liveout_renames (scop_p scop, htab_t rename_map)
4448 {
4449   int i;
4450   sese region = SCOP_REGION (scop);
4451
4452   for (i = 0; i < SESE_NUM_VER (region); i++)
4453     if (bitmap_bit_p (SESE_LIVEOUT (region), i)
4454         && is_gimple_reg (ssa_name (i)))
4455       {
4456         tree old_name = ssa_name (i);
4457         tree new_name = get_new_name_from_old_name (rename_map, old_name);
4458
4459         register_old_and_new_names (SCOP_LIVEOUT_RENAMES (scop),
4460                                     old_name, new_name);
4461       }
4462 }
4463
4464 /* Copies BB and includes in the copied BB all the statements that can
4465    be reached following the use-def chains from the memory accesses,
4466    and returns the next edge following this new block.  */
4467  
4468 static edge
4469 copy_bb_and_scalar_dependences (basic_block bb, scop_p scop,
4470                                 edge next_e, htab_t map)
4471 {
4472   basic_block new_bb = split_edge (next_e);
4473
4474   next_e = single_succ_edge (new_bb);
4475   graphite_copy_stmts_from_block (bb, new_bb, map);
4476   remove_condition (new_bb);
4477   rename_variables (new_bb, map);
4478   remove_phi_nodes (new_bb);
4479   expand_scalar_variables (new_bb, scop, map);
4480   register_scop_liveout_renames (scop, map);
4481
4482   return next_e;
4483 }
4484
4485 /* Helper function for htab_traverse in insert_loop_close_phis.  */
4486
4487 static int
4488 add_loop_exit_phis (void **slot, void *s)
4489 {
4490   struct rename_map_elt *entry = (struct rename_map_elt *) *slot;
4491   tree new_name = entry->new_name;
4492   basic_block bb = (basic_block) s;
4493   gimple phi = create_phi_node (new_name, bb);
4494   tree res = create_new_def_for (gimple_phi_result (phi), phi,
4495                                  gimple_phi_result_ptr (phi));
4496
4497   add_phi_arg (phi, new_name, single_pred_edge (bb));
4498
4499   entry->new_name = res;
4500   *slot = entry;
4501   return 1;
4502 }
4503
4504 /* Iterate over the SCOP_LIVEOUT_RENAMES (SCOP) and get tuples of the
4505    form (OLD_NAME, NEW_NAME).  Insert in BB "RES = phi (NEW_NAME)",
4506    and finally register in SCOP_LIVEOUT_RENAMES (scop) the tuple
4507    (OLD_NAME, RES).  */
4508
4509 static void
4510 insert_loop_close_phis (scop_p scop, basic_block bb)
4511 {
4512   update_ssa (TODO_update_ssa);
4513   htab_traverse (SCOP_LIVEOUT_RENAMES (scop), add_loop_exit_phis, bb);
4514   update_ssa (TODO_update_ssa);
4515 }
4516
4517 /* Helper structure for htab_traverse in insert_guard_phis.  */
4518
4519 struct igp {
4520   basic_block bb;
4521   edge true_edge, false_edge;
4522   htab_t liveout_before_guard;
4523 };
4524
4525 /* Return the default name that is before the guard.  */
4526
4527 static tree
4528 default_liveout_before_guard (htab_t liveout_before_guard, tree old_name)
4529 {
4530   tree res = get_new_name_from_old_name (liveout_before_guard, old_name);
4531
4532   if (res == old_name)
4533     {
4534       if (is_gimple_reg (res))
4535         return fold_convert (TREE_TYPE (res), integer_zero_node);
4536       return gimple_default_def (cfun, res);
4537     }
4538
4539   return res;
4540 }
4541
4542 /* Helper function for htab_traverse in insert_guard_phis.  */
4543
4544 static int
4545 add_guard_exit_phis (void **slot, void *s)
4546 {
4547   struct rename_map_elt *entry = (struct rename_map_elt *) *slot;
4548   struct igp *i = (struct igp *) s;
4549   basic_block bb = i->bb;
4550   edge true_edge = i->true_edge;
4551   edge false_edge = i->false_edge;
4552   tree name1 = entry->new_name;
4553   tree name2 = default_liveout_before_guard (i->liveout_before_guard,
4554                                              entry->old_name);
4555   gimple phi = create_phi_node (name1, bb);
4556   tree res = create_new_def_for (gimple_phi_result (phi), phi,
4557                                  gimple_phi_result_ptr (phi));
4558
4559   add_phi_arg (phi, name1, true_edge);
4560   add_phi_arg (phi, name2, false_edge);
4561
4562   entry->new_name = res;
4563   *slot = entry;
4564   return 1;
4565 }
4566
4567 /* Iterate over the SCOP_LIVEOUT_RENAMES (SCOP) and get tuples of the
4568    form (OLD_NAME, NAME1).  If there is a correspondent tuple of
4569    OLD_NAME in LIVEOUT_BEFORE_GUARD, i.e. (OLD_NAME, NAME2) then
4570    insert in BB
4571    
4572    | RES = phi (NAME1 (on TRUE_EDGE), NAME2 (on FALSE_EDGE))"
4573
4574    if there is no tuple for OLD_NAME in LIVEOUT_BEFORE_GUARD, insert
4575
4576    | RES = phi (NAME1 (on TRUE_EDGE),
4577    |            DEFAULT_DEFINITION of NAME1 (on FALSE_EDGE))".
4578
4579    Finally register in SCOP_LIVEOUT_RENAMES (scop) the tuple
4580    (OLD_NAME, RES).  */
4581
4582 static void
4583 insert_guard_phis (scop_p scop, basic_block bb, edge true_edge,
4584                    edge false_edge, htab_t liveout_before_guard)
4585 {
4586   struct igp i;
4587   i.bb = bb;
4588   i.true_edge = true_edge;
4589   i.false_edge = false_edge;
4590   i.liveout_before_guard = liveout_before_guard;
4591
4592   update_ssa (TODO_update_ssa);
4593   htab_traverse (SCOP_LIVEOUT_RENAMES (scop), add_guard_exit_phis, &i);
4594   update_ssa (TODO_update_ssa);
4595 }
4596
4597 /* Helper function for htab_traverse.  */
4598
4599 static int
4600 copy_renames (void **slot, void *s)
4601 {
4602   struct rename_map_elt *entry = (struct rename_map_elt *) *slot;
4603   htab_t res = (htab_t) s;
4604   tree old_name = entry->old_name;
4605   tree new_name = entry->new_name;
4606   struct rename_map_elt tmp;
4607   PTR *x;
4608
4609   tmp.old_name = old_name;
4610   x = htab_find_slot (res, &tmp, INSERT);
4611
4612   if (!*x)
4613     *x = new_rename_map_elt (old_name, new_name);
4614
4615   return 1;
4616 }
4617
4618 /* Translates a CLAST statement STMT to GCC representation in the
4619    context of a SCOP.
4620
4621    - NEXT_E is the edge where new generated code should be attached.
4622    - CONTEXT_LOOP is the loop in which the generated code will be placed
4623      (might be NULL).  
4624    - IVSTACK contains the surrounding loops around the statement to be
4625      translated.
4626 */
4627
4628 static edge
4629 translate_clast (scop_p scop, struct loop *context_loop,
4630                  struct clast_stmt *stmt, edge next_e, loop_iv_stack ivstack)
4631 {
4632   if (!stmt)
4633     return next_e;
4634
4635   if (CLAST_STMT_IS_A (stmt, stmt_root))
4636     return translate_clast (scop, context_loop, stmt->next, next_e, ivstack);
4637
4638   if (CLAST_STMT_IS_A (stmt, stmt_user))
4639     {
4640       htab_t map;
4641       CloogStatement *cs = ((struct clast_user_stmt *) stmt)->statement;
4642       graphite_bb_p gbb = (graphite_bb_p) cloog_statement_usr (cs);
4643
4644       if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
4645         return next_e;
4646
4647       map = htab_create (10, rename_map_elt_info, eq_rename_map_elts, free);
4648       loop_iv_stack_patch_for_consts (ivstack, (struct clast_user_stmt *) stmt);
4649       build_iv_mapping (ivstack, map, gbb, scop);
4650       next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), scop,
4651                                                next_e, map);
4652       htab_delete (map);
4653       loop_iv_stack_remove_constants (ivstack);
4654       update_ssa (TODO_update_ssa);
4655       recompute_all_dominators ();
4656       graphite_verify ();
4657       return translate_clast (scop, context_loop, stmt->next, next_e, ivstack);
4658     }
4659
4660   if (CLAST_STMT_IS_A (stmt, stmt_for))
4661     {
4662       struct loop *loop
4663         = graphite_create_new_loop (scop, next_e, (struct clast_for *) stmt,
4664                                     ivstack, context_loop ? context_loop
4665                                     : get_loop (0));
4666       edge last_e = single_exit (loop);
4667
4668       next_e = translate_clast (scop, loop, ((struct clast_for *) stmt)->body,
4669                                 single_pred_edge (loop->latch), ivstack);
4670       redirect_edge_succ_nodup (next_e, loop->latch);
4671
4672       set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
4673       loop_iv_stack_pop (ivstack);
4674       last_e = single_succ_edge (split_edge (last_e));
4675       insert_loop_close_phis (scop, last_e->src);
4676
4677       recompute_all_dominators ();
4678       graphite_verify ();
4679       return translate_clast (scop, context_loop, stmt->next, last_e, ivstack);
4680     }
4681
4682   if (CLAST_STMT_IS_A (stmt, stmt_guard))
4683     {
4684       htab_t liveout_before_guard = htab_create (10, rename_map_elt_info,
4685                                                  eq_rename_map_elts, free);
4686       edge last_e = graphite_create_new_guard (scop, next_e,
4687                                                ((struct clast_guard *) stmt),
4688                                                ivstack);
4689       edge true_e = get_true_edge_from_guard_bb (next_e->dest);
4690       edge false_e = get_false_edge_from_guard_bb (next_e->dest);
4691       edge exit_true_e = single_succ_edge (true_e->dest);
4692       edge exit_false_e = single_succ_edge (false_e->dest);
4693
4694       htab_traverse (SCOP_LIVEOUT_RENAMES (scop), copy_renames,
4695                      liveout_before_guard);
4696
4697       next_e = translate_clast (scop, context_loop, 
4698                                 ((struct clast_guard *) stmt)->then,
4699                                 true_e, ivstack);
4700       insert_guard_phis (scop, last_e->src, exit_true_e, exit_false_e,
4701                          liveout_before_guard);
4702       htab_delete (liveout_before_guard);
4703       recompute_all_dominators ();
4704       graphite_verify ();
4705
4706       return translate_clast (scop, context_loop, stmt->next, last_e, ivstack);
4707     }
4708
4709   if (CLAST_STMT_IS_A (stmt, stmt_block))
4710     {
4711       next_e = translate_clast (scop, context_loop,
4712                                 ((struct clast_block *) stmt)->body,
4713                                 next_e, ivstack);
4714       recompute_all_dominators ();
4715       graphite_verify ();
4716       return translate_clast (scop, context_loop, stmt->next, next_e, ivstack);
4717     }
4718
4719   gcc_unreachable ();
4720 }
4721
4722 /* Free the SCATTERING domain list.  */
4723
4724 static void
4725 free_scattering (CloogDomainList *scattering)
4726 {
4727   while (scattering)
4728     {
4729       CloogDomain *dom = cloog_domain (scattering);
4730       CloogDomainList *next = cloog_next_domain (scattering);
4731
4732       cloog_domain_free (dom);
4733       free (scattering);
4734       scattering = next;
4735     }
4736 }
4737
4738 /* Build cloog program for SCoP.  */
4739
4740 static void
4741 build_cloog_prog (scop_p scop)
4742 {
4743   int i;
4744   int max_nb_loops = scop_max_loop_depth (scop);
4745   graphite_bb_p gbb;
4746   CloogLoop *loop_list = NULL;
4747   CloogBlockList *block_list = NULL;
4748   CloogDomainList *scattering = NULL;
4749   CloogProgram *prog = SCOP_PROG (scop);
4750   int nbs = 2 * max_nb_loops + 1;
4751   int *scaldims = (int *) xmalloc (nbs * (sizeof (int)));
4752
4753   cloog_program_set_nb_scattdims (prog, nbs);
4754   initialize_cloog_names (scop);
4755
4756   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++)
4757     {
4758       /* Build new block.  */
4759       CloogMatrix *domain = GBB_DOMAIN (gbb);
4760       CloogStatement *stmt = cloog_statement_alloc (GBB_BB (gbb)->index);
4761       CloogBlock *block = cloog_block_alloc (stmt, 0, NULL,
4762                                              nb_loops_around_gb (gbb));
4763       cloog_statement_set_usr (stmt, gbb);
4764
4765       /* Add empty domain to all bbs, which do not yet have a domain, as they
4766          are not part of any loop.  */
4767       if (domain == NULL)
4768         {
4769           domain = cloog_matrix_alloc (0, scop_nb_params (scop) + 2);
4770           GBB_DOMAIN (gbb) = domain;
4771         }
4772
4773       /* Build loop list.  */
4774       {
4775         CloogLoop *new_loop_list = cloog_loop_malloc ();
4776         cloog_loop_set_next (new_loop_list, loop_list);
4777         cloog_loop_set_domain (new_loop_list,
4778                                cloog_domain_matrix2domain (domain));
4779         cloog_loop_set_block (new_loop_list, block);
4780         loop_list = new_loop_list;
4781       }
4782
4783       /* Build block list.  */
4784       {
4785         CloogBlockList *new_block_list = cloog_block_list_malloc ();
4786
4787         cloog_block_list_set_next (new_block_list, block_list);
4788         cloog_block_list_set_block (new_block_list, block);
4789         block_list = new_block_list;
4790       }
4791
4792       /* Build scattering list.  */
4793       {
4794         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
4795         CloogDomainList *new_scattering
4796           = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
4797         CloogMatrix *scat_mat = schedule_to_scattering (gbb, nbs);
4798
4799         cloog_set_next_domain (new_scattering, scattering);
4800         cloog_set_domain (new_scattering,
4801                           cloog_domain_matrix2domain (scat_mat));
4802         scattering = new_scattering;
4803         cloog_matrix_free (scat_mat);
4804       }
4805     }
4806
4807   cloog_program_set_loop (prog, loop_list);
4808   cloog_program_set_blocklist (prog, block_list);
4809
4810   for (i = 0; i < nbs; i++)
4811     scaldims[i] = 0 ;
4812
4813   cloog_program_set_scaldims (prog, scaldims);
4814
4815   /* Extract scalar dimensions to simplify the code generation problem.  */
4816   cloog_program_extract_scalars (prog, scattering);
4817
4818   /* Apply scattering.  */
4819   cloog_program_scatter (prog, scattering);
4820   free_scattering (scattering);
4821
4822   /* Iterators corresponding to scalar dimensions have to be extracted.  */
4823   cloog_names_scalarize (cloog_program_names (prog), nbs,
4824                          cloog_program_scaldims (prog));
4825   
4826   /* Free blocklist.  */
4827   {
4828     CloogBlockList *next = cloog_program_blocklist (prog);
4829         
4830     while (next)
4831       {
4832         CloogBlockList *toDelete = next;
4833         next = cloog_block_list_next (next);
4834         cloog_block_list_set_next (toDelete, NULL);
4835         cloog_block_list_set_block (toDelete, NULL);
4836         cloog_block_list_free (toDelete);
4837       }
4838     cloog_program_set_blocklist (prog, NULL);
4839   }
4840 }
4841
4842 /* Return the options that will be used in GLOOG.  */
4843
4844 static CloogOptions *
4845 set_cloog_options (void)
4846 {
4847   CloogOptions *options = cloog_options_malloc ();
4848
4849   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
4850      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
4851      we pass an incomplete program to cloog.  */
4852   options->language = LANGUAGE_C;
4853
4854   /* Enable complex equality spreading: removes dummy statements
4855      (assignments) in the generated code which repeats the
4856      substitution equations for statements.  This is useless for
4857      GLooG.  */
4858   options->esp = 1;
4859
4860   /* Enable C pretty-printing mode: normalizes the substitution
4861      equations for statements.  */
4862   options->cpp = 1;
4863
4864   /* Allow cloog to build strides with a stride width different to one.
4865      This example has stride = 4:
4866
4867      for (i = 0; i < 20; i += 4)
4868        A  */
4869   options->strides = 1;
4870
4871   /* Disable optimizations and make cloog generate source code closer to the
4872      input.  This is useful for debugging,  but later we want the optimized
4873      code.
4874
4875      XXX: We can not disable optimizations, as loop blocking is not working
4876      without them.  */
4877   if (0)
4878     {
4879       options->f = -1;
4880       options->l = INT_MAX;
4881     }
4882
4883   return options;
4884 }
4885
4886 /* Prints STMT to STDERR.  */
4887
4888 void
4889 debug_clast_stmt (struct clast_stmt *stmt)
4890 {
4891   CloogOptions *options = set_cloog_options ();
4892
4893   pprint (stderr, stmt, 0, options);
4894 }
4895
4896 /* Find the right transform for the SCOP, and return a Cloog AST
4897    representing the new form of the program.  */
4898
4899 static struct clast_stmt *
4900 find_transform (scop_p scop)
4901 {
4902   struct clast_stmt *stmt;
4903   CloogOptions *options = set_cloog_options ();
4904
4905   /* Connect new cloog prog generation to graphite.  */
4906   build_cloog_prog (scop);
4907
4908   if (dump_file && (dump_flags & TDF_DETAILS))
4909     {
4910       fprintf (dump_file, "Cloog Input [\n");
4911       cloog_program_print (dump_file, SCOP_PROG(scop));
4912       fprintf (dump_file, "]\n");
4913     }
4914
4915   SCOP_PROG (scop) = cloog_program_generate (SCOP_PROG (scop), options);
4916   stmt = cloog_clast_create (SCOP_PROG (scop), options);
4917
4918   if (dump_file && (dump_flags & TDF_DETAILS))
4919     {
4920       fprintf (dump_file, "Cloog Output[\n");
4921       pprint (dump_file, stmt, 0, options);
4922       cloog_program_dump_cloog (dump_file, SCOP_PROG (scop));
4923       fprintf (dump_file, "]\n");
4924     }
4925
4926   cloog_options_free (options);
4927   return stmt;
4928 }
4929
4930 /* Remove from the CFG the REGION.  */
4931
4932 static inline void
4933 remove_sese_region (sese region)
4934 {
4935   VEC (basic_block, heap) *bbs = NULL;
4936   basic_block entry_bb = SESE_ENTRY (region)->dest;
4937   basic_block exit_bb = SESE_EXIT (region)->dest;
4938   basic_block bb;
4939   int i;
4940
4941   VEC_safe_push (basic_block, heap, bbs, entry_bb);
4942   gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
4943
4944   for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
4945     delete_basic_block (bb);
4946
4947   VEC_free (basic_block, heap, bbs);
4948 }
4949
4950 typedef struct ifsese {
4951   sese region;
4952   sese true_region;
4953   sese false_region;
4954 } *ifsese;
4955
4956 static inline edge
4957 if_region_entry (ifsese if_region)
4958 {
4959   return SESE_ENTRY (if_region->region);
4960 }
4961
4962 static inline edge
4963 if_region_exit (ifsese if_region)
4964 {
4965   return SESE_EXIT (if_region->region);
4966 }
4967
4968 static inline basic_block
4969 if_region_get_condition_block (ifsese if_region)
4970 {
4971   return if_region_entry (if_region)->dest;
4972 }
4973
4974 static inline void
4975 if_region_set_false_region (ifsese if_region, sese region)
4976 {
4977   basic_block condition = if_region_get_condition_block (if_region);
4978   edge false_edge = get_false_edge_from_guard_bb (condition);
4979   edge entry_region = SESE_ENTRY (region);
4980   edge exit_region = SESE_EXIT (region);
4981   basic_block before_region = entry_region->src;
4982   basic_block last_in_region = exit_region->src;
4983   void **slot = htab_find_slot_with_hash (current_loops->exits, exit_region,
4984                                           htab_hash_pointer (exit_region),
4985                                           NO_INSERT);
4986
4987   entry_region->flags = false_edge->flags;
4988   false_edge->flags = exit_region->flags;
4989
4990   redirect_edge_pred (entry_region, condition);
4991   redirect_edge_pred (exit_region, before_region);
4992   redirect_edge_pred (false_edge, last_in_region);
4993
4994   exit_region->flags = EDGE_FALLTHRU;
4995   recompute_all_dominators ();
4996
4997   SESE_EXIT (region) = single_succ_edge (false_edge->dest);
4998   if_region->false_region = region;
4999
5000   if (slot)
5001     {
5002       struct loop_exit *loop_exit = GGC_CNEW (struct loop_exit);
5003
5004       memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
5005       htab_clear_slot (current_loops->exits, slot);
5006
5007       slot = htab_find_slot_with_hash (current_loops->exits, false_edge,
5008                                        htab_hash_pointer (false_edge),
5009                                        INSERT);
5010       loop_exit->e = false_edge;
5011       *slot = loop_exit;
5012       false_edge->src->loop_father->exits->next = loop_exit;
5013     }
5014 }
5015
5016 static ifsese
5017 create_if_region_on_edge (edge entry, tree condition)
5018 {
5019   edge e;
5020   edge_iterator ei;
5021   sese sese_region = GGC_NEW (struct sese);
5022   sese true_region = GGC_NEW (struct sese);
5023   sese false_region = GGC_NEW (struct sese);
5024   ifsese if_region = GGC_NEW (struct ifsese);
5025   edge exit = create_empty_if_region_on_edge (entry, condition);
5026
5027   if_region->region = sese_region;
5028   if_region->region->entry = entry;
5029   if_region->region->exit = exit;
5030
5031   FOR_EACH_EDGE (e, ei, entry->dest->succs)
5032     {
5033       if (e->flags & EDGE_TRUE_VALUE)
5034         {
5035           true_region->entry = e;
5036           true_region->exit = single_succ_edge (e->dest);
5037           if_region->true_region = true_region;
5038         }
5039       else if (e->flags & EDGE_FALSE_VALUE)
5040         {
5041           false_region->entry = e;
5042           false_region->exit = single_succ_edge (e->dest);
5043           if_region->false_region = false_region;
5044         }
5045     }
5046
5047   return if_region;
5048 }
5049
5050 /* Moves REGION in a condition expression:
5051    | if (1)
5052    |   ;
5053    | else
5054    |   REGION;
5055 */
5056
5057 static ifsese
5058 move_sese_in_condition (sese region)
5059 {
5060   basic_block pred_block = split_edge (SESE_ENTRY (region));
5061   ifsese if_region = NULL;
5062
5063   SESE_ENTRY (region) = single_succ_edge (pred_block);
5064   if_region = create_if_region_on_edge (single_pred_edge (pred_block), integer_one_node);
5065   if_region_set_false_region (if_region, region);
5066
5067   return if_region;
5068 }
5069
5070 /* Add exit phis for USE on EXIT.  */
5071
5072 static void
5073 scop_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
5074 {
5075   gimple phi = create_phi_node (use, exit);
5076
5077   create_new_def_for (gimple_phi_result (phi), phi,
5078                       gimple_phi_result_ptr (phi));
5079   add_phi_arg (phi, use, false_e);
5080   add_phi_arg (phi, use, true_e);
5081 }
5082
5083 /* Add phi nodes for VAR that is used in LIVEIN.  Phi nodes are
5084    inserted in block BB.  */
5085
5086 static void
5087 scop_add_exit_phis_var (basic_block bb, tree var, bitmap livein,
5088                         edge false_e, edge true_e)
5089 {
5090   bitmap def;
5091   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
5092
5093   if (is_gimple_reg (var))
5094     bitmap_clear_bit (livein, def_bb->index);
5095   else
5096     bitmap_set_bit (livein, def_bb->index);
5097
5098   def = BITMAP_ALLOC (NULL);
5099   bitmap_set_bit (def, def_bb->index);
5100   compute_global_livein (livein, def);
5101   BITMAP_FREE (def);
5102
5103   scop_add_exit_phis_edge (bb, var, false_e, true_e);
5104 }
5105
5106 /* Insert in the block BB phi nodes for variables defined in REGION
5107    and used outside the REGION.  The code generation moves REGION in
5108    the else clause of an "if (1)" and generates code in the then
5109    clause that is at this point empty:
5110
5111    | if (1)
5112    |   empty;
5113    | else
5114    |   REGION;
5115 */
5116
5117 static void
5118 scop_insert_phis_for_liveouts (sese region, basic_block bb,
5119                                edge false_e, edge true_e)
5120 {
5121   unsigned i;
5122   bitmap_iterator bi;
5123
5124   update_ssa (TODO_update_ssa);
5125
5126   EXECUTE_IF_SET_IN_BITMAP (SESE_LIVEOUT (region), 0, i, bi)
5127     scop_add_exit_phis_var (bb, ssa_name (i), SESE_LIVEIN_VER (region, i),
5128                             false_e, true_e);
5129
5130   update_ssa (TODO_update_ssa);
5131 }
5132
5133 /* Get the definition of NAME before the SCOP.  Keep track of the
5134    basic blocks that have been VISITED in a bitmap.  */
5135
5136 static tree
5137 get_vdef_before_scop (scop_p scop, tree name, sbitmap visited)
5138 {
5139   unsigned i;
5140   gimple def_stmt = SSA_NAME_DEF_STMT (name);
5141   basic_block def_bb = gimple_bb (def_stmt);
5142
5143   if (!def_bb
5144       || !bb_in_scop_p (def_bb, scop))
5145     return name;
5146
5147   if (TEST_BIT (visited, def_bb->index))
5148     return NULL_TREE;
5149
5150   SET_BIT (visited, def_bb->index);
5151
5152   switch (gimple_code (def_stmt))
5153     {
5154     case GIMPLE_PHI:
5155       for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
5156         {
5157           tree arg = gimple_phi_arg_def (def_stmt, i);
5158           tree res = get_vdef_before_scop (scop, arg, visited);
5159           if (res)
5160             return res;
5161         }
5162       return NULL_TREE;
5163
5164     default:
5165       return NULL_TREE;
5166     }
5167 }
5168
5169 /* Adjust a virtual phi node PHI that is placed at the end of the
5170    generated code for SCOP:
5171
5172    | if (1)
5173    |   generated code from REGION;
5174    | else
5175    |   REGION;
5176
5177    The FALSE_E edge comes from the original code, TRUE_E edge comes
5178    from the code generated for the SCOP.  */
5179
5180 static void
5181 scop_adjust_vphi (scop_p scop, gimple phi, edge true_e)
5182 {
5183   unsigned i;
5184
5185   gcc_assert (gimple_phi_num_args (phi) == 2);
5186
5187   for (i = 0; i < gimple_phi_num_args (phi); i++)
5188     if (gimple_phi_arg_edge (phi, i) == true_e)
5189       {
5190         tree true_arg, false_arg, before_scop_arg;
5191         sbitmap visited;
5192
5193         true_arg = gimple_phi_arg_def (phi, i);
5194         if (!SSA_NAME_IS_DEFAULT_DEF (true_arg))
5195           return;
5196
5197         false_arg = gimple_phi_arg_def (phi, i == 0 ? 1 : 0);
5198         if (SSA_NAME_IS_DEFAULT_DEF (false_arg))
5199           return;
5200
5201         visited = sbitmap_alloc (last_basic_block);
5202         sbitmap_zero (visited);
5203         before_scop_arg = get_vdef_before_scop (scop, false_arg, visited);
5204         gcc_assert (before_scop_arg != NULL_TREE);
5205         SET_PHI_ARG_DEF (phi, i, before_scop_arg);
5206         sbitmap_free (visited);
5207       }
5208 }
5209
5210 /* Adjusts the phi nodes in the block BB for variables defined in
5211    SCOP_REGION and used outside the SCOP_REGION.  The code generation
5212    moves SCOP_REGION in the else clause of an "if (1)" and generates
5213    code in the then clause:
5214
5215    | if (1)
5216    |   generated code from REGION;
5217    | else
5218    |   REGION;
5219
5220    To adjust the phi nodes after the condition, SCOP_LIVEOUT_RENAMES
5221    hash table is used: this stores for a name that is part of the
5222    LIVEOUT of SCOP_REGION its new name in the generated code.  */
5223
5224 static void
5225 scop_adjust_phis_for_liveouts (scop_p scop, basic_block bb, edge false_e,
5226                                edge true_e)
5227 {
5228   gimple_stmt_iterator si;
5229
5230   for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5231     {
5232       unsigned i, false_i;
5233       gimple phi = gsi_stmt (si);
5234
5235       if (!is_gimple_reg (PHI_RESULT (phi)))
5236         {
5237           scop_adjust_vphi (scop, phi, true_e);
5238           continue;
5239         }
5240
5241       for (i = 0; i < gimple_phi_num_args (phi); i++)
5242         if (gimple_phi_arg_edge (phi, i) == false_e)
5243           {
5244             false_i = i;
5245             break;
5246           }
5247
5248       for (i = 0; i < gimple_phi_num_args (phi); i++)
5249         if (gimple_phi_arg_edge (phi, i) == true_e)
5250           {
5251             tree old_name = gimple_phi_arg_def (phi, false_i);
5252             tree new_name = get_new_name_from_old_name
5253               (SCOP_LIVEOUT_RENAMES (scop), old_name);
5254
5255             gcc_assert (old_name != new_name);
5256             SET_PHI_ARG_DEF (phi, i, new_name);
5257           }
5258     }
5259 }
5260
5261 /* Returns the first cloog name used in EXPR.  */
5262
5263 static const char *
5264 find_cloog_iv_in_expr (struct clast_expr *expr)
5265 {
5266   struct clast_term *term = (struct clast_term *) expr;
5267
5268   if (expr->type == expr_term
5269       && !term->var)
5270     return NULL;
5271
5272   if (expr->type == expr_term)
5273     return term->var;
5274
5275   if (expr->type == expr_red)
5276     {
5277       int i;
5278       struct clast_reduction *red = (struct clast_reduction *) expr;
5279
5280       for (i = 0; i < red->n; i++)
5281         {
5282           const char *res = find_cloog_iv_in_expr ((red)->elts[i]);
5283
5284           if (res)
5285             return res;
5286         }
5287     }
5288
5289   return NULL;
5290 }
5291
5292 /* Build for a clast_user_stmt USER_STMT a map between the CLAST
5293    induction variables and the corresponding GCC old induction
5294    variables.  This information is stored on each GRAPHITE_BB.  */
5295
5296 static void
5297 compute_cloog_iv_types_1 (graphite_bb_p gbb,
5298                           struct clast_user_stmt *user_stmt)
5299 {
5300   struct clast_stmt *t;
5301   int index = 0;
5302
5303   for (t = user_stmt->substitutions; t; t = t->next, index++)
5304     {
5305       PTR *slot;
5306       struct ivtype_map_elt tmp;
5307       struct clast_expr *expr = (struct clast_expr *) 
5308         ((struct clast_assignment *)t)->RHS;
5309
5310       /* Create an entry (clast_var, type).  */
5311       tmp.cloog_iv = find_cloog_iv_in_expr (expr);
5312       if (!tmp.cloog_iv)
5313         continue;
5314
5315       slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, INSERT);
5316
5317       if (!*slot)
5318         {
5319           loop_p loop = gbb_loop_at_index (gbb, index);
5320           tree oldiv = oldiv_for_loop (GBB_SCOP (gbb), loop);
5321           tree type = oldiv ? TREE_TYPE (oldiv) : integer_type_node;
5322           *slot = new_ivtype_map_elt (tmp.cloog_iv, type);
5323         }
5324     }
5325 }
5326
5327 /* Walk the CLAST tree starting from STMT and build for each
5328    clast_user_stmt a map between the CLAST induction variables and the
5329    corresponding GCC old induction variables.  This information is
5330    stored on each GRAPHITE_BB.  */
5331
5332 static void
5333 compute_cloog_iv_types (struct clast_stmt *stmt)
5334 {
5335   if (!stmt)
5336     return;
5337
5338   if (CLAST_STMT_IS_A (stmt, stmt_root))
5339     goto next;
5340
5341   if (CLAST_STMT_IS_A (stmt, stmt_user))
5342     {
5343       CloogStatement *cs = ((struct clast_user_stmt *) stmt)->statement;
5344       graphite_bb_p gbb = (graphite_bb_p) cloog_statement_usr (cs);
5345       GBB_CLOOG_IV_TYPES (gbb) = htab_create (10, ivtype_map_elt_info,
5346                                               eq_ivtype_map_elts, free);
5347       compute_cloog_iv_types_1 (gbb, (struct clast_user_stmt *) stmt);
5348       goto next;
5349     }
5350
5351   if (CLAST_STMT_IS_A (stmt, stmt_for))
5352     {
5353       struct clast_stmt *s = ((struct clast_for *) stmt)->body;
5354       compute_cloog_iv_types (s);
5355       goto next;
5356     }
5357
5358   if (CLAST_STMT_IS_A (stmt, stmt_guard))
5359     {
5360       struct clast_stmt *s = ((struct clast_guard *) stmt)->then;
5361       compute_cloog_iv_types (s);
5362       goto next;
5363     }
5364
5365   if (CLAST_STMT_IS_A (stmt, stmt_block))
5366     {
5367       struct clast_stmt *s = ((struct clast_block *) stmt)->body;
5368       compute_cloog_iv_types (s);
5369       goto next;
5370     }
5371
5372   gcc_unreachable ();
5373
5374  next:
5375   compute_cloog_iv_types (stmt->next);
5376 }
5377
5378 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
5379    the given SCOP.  */
5380
5381 static void
5382 gloog (scop_p scop, struct clast_stmt *stmt)
5383 {
5384   edge new_scop_exit_edge = NULL;
5385   VEC (iv_stack_entry_p, heap) *ivstack = VEC_alloc (iv_stack_entry_p, heap,
5386                                                      10);
5387   loop_p context_loop;
5388   ifsese if_region = NULL;
5389
5390   if_region = move_sese_in_condition (SCOP_REGION (scop));
5391   sese_build_livein_liveouts (SCOP_REGION (scop));
5392   scop_insert_phis_for_liveouts (SCOP_REGION (scop),
5393                                  if_region->region->exit->src,
5394                                  if_region->false_region->exit,
5395                                  if_region->true_region->exit);
5396   recompute_all_dominators ();
5397   graphite_verify ();
5398   context_loop = SESE_ENTRY (SCOP_REGION (scop))->src->loop_father;
5399   compute_cloog_iv_types (stmt);
5400
5401   new_scop_exit_edge = translate_clast (scop, context_loop, stmt,
5402                                         if_region->true_region->entry,
5403                                         &ivstack);
5404   free_loop_iv_stack (&ivstack);
5405   cloog_clast_free (stmt);
5406
5407   graphite_verify ();
5408   scop_adjust_phis_for_liveouts (scop,
5409                                  if_region->region->exit->src,
5410                                  if_region->false_region->exit,
5411                                  if_region->true_region->exit);
5412
5413   recompute_all_dominators ();
5414   graphite_verify ();
5415 }
5416
5417 /* Returns the number of data references in SCOP.  */
5418
5419 static int
5420 nb_data_refs_in_scop (scop_p scop)
5421 {
5422   int i;
5423   graphite_bb_p gbb;
5424   int res = 0;
5425
5426   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gbb); i++)
5427     res += VEC_length (data_reference_p, GBB_DATA_REFS (gbb));
5428
5429   return res;
5430 }
5431
5432 /* Move the loop at index LOOP and insert it before index NEW_LOOP_POS.
5433    This transformartion is only valid, if the loop nest between i and k is
5434    perfectly nested. Therefore we do not need to change the static schedule.
5435
5436    Example:
5437
5438    for (i = 0; i < 50; i++)
5439      for (j ...)
5440        for (k = 5; k < 100; k++)
5441          A
5442
5443    To move k before i use:
5444
5445    graphite_trans_bb_move_loop (A, 2, 0)
5446
5447    for (k = 5; k < 100; k++)
5448      for (i = 0; i < 50; i++)
5449        for (j ...)
5450          A
5451
5452    And to move k back:
5453
5454    graphite_trans_bb_move_loop (A, 0, 2)
5455
5456    This function does not check the validity of interchanging loops.
5457    This should be checked before calling this function.  */
5458
5459 static void
5460 graphite_trans_bb_move_loop (graphite_bb_p gb, int loop,
5461                              int new_loop_pos)
5462 {
5463   CloogMatrix *domain = GBB_DOMAIN (gb);
5464   int row, j;
5465   loop_p tmp_loop_p;
5466
5467   gcc_assert (loop < gbb_nb_loops (gb)
5468               && new_loop_pos < gbb_nb_loops (gb));
5469
5470   /* Update LOOPS vector.  */
5471   tmp_loop_p = VEC_index (loop_p, GBB_LOOPS (gb), loop);
5472   VEC_ordered_remove (loop_p, GBB_LOOPS (gb), loop);
5473   VEC_safe_insert (loop_p, heap, GBB_LOOPS (gb), new_loop_pos, tmp_loop_p);
5474
5475   /* Move the domain columns.  */
5476   if (loop < new_loop_pos)
5477     for (row = 0; row < domain->NbRows; row++)
5478       {
5479         Value tmp;
5480         value_init (tmp);
5481         value_assign (tmp, domain->p[row][loop + 1]);
5482    
5483         for (j = loop ; j < new_loop_pos - 1; j++)
5484           value_assign (domain->p[row][j + 1], domain->p[row][j + 2]);
5485
5486         value_assign (domain->p[row][new_loop_pos], tmp);
5487         value_clear (tmp);
5488       }
5489   else
5490     for (row = 0; row < domain->NbRows; row++)
5491       {
5492         Value tmp;
5493         value_init (tmp);
5494         value_assign (tmp, domain->p[row][loop + 1]);
5495
5496         for (j = loop ; j > new_loop_pos; j--)
5497           value_assign (domain->p[row][j + 1], domain->p[row][j]);
5498      
5499         value_assign (domain->p[row][new_loop_pos + 1], tmp);
5500         value_clear (tmp);
5501       }
5502 }
5503
5504 /* Get the index of the column representing constants in the DOMAIN
5505    matrix.  */
5506
5507 static int
5508 const_column_index (CloogMatrix *domain)
5509 {
5510   return domain->NbColumns - 1;
5511 }
5512
5513
5514 /* Get the first index that is positive or negative, determined
5515    following the value of POSITIVE, in matrix DOMAIN in COLUMN.  */
5516
5517 static int
5518 get_first_matching_sign_row_index (CloogMatrix *domain, int column,
5519                                    bool positive)
5520 {
5521   int row;
5522
5523   for (row = 0; row < domain->NbRows; row++)
5524     {
5525       int val = value_get_si (domain->p[row][column]);
5526
5527       if (val > 0 && positive)
5528         return row;
5529
5530       else if (val < 0 && !positive)
5531         return row;
5532     }
5533
5534   gcc_unreachable ();
5535 }
5536
5537 /* Get the lower bound of COLUMN in matrix DOMAIN.  */
5538
5539 static int
5540 get_lower_bound_row (CloogMatrix *domain, int column)
5541 {
5542   return get_first_matching_sign_row_index (domain, column, true);
5543 }
5544
5545 /* Get the upper bound of COLUMN in matrix DOMAIN.  */
5546
5547 static int
5548 get_upper_bound_row (CloogMatrix *domain, int column)
5549 {
5550   return get_first_matching_sign_row_index (domain, column, false);
5551 }
5552
5553 /* Copies the OLD_ROW constraint from OLD_DOMAIN to the NEW_DOMAIN at
5554    row NEW_ROW.  */
5555
5556 static void
5557 copy_constraint (CloogMatrix *old_domain, CloogMatrix *new_domain,
5558                  int old_row, int new_row)
5559 {
5560   int i;
5561
5562   gcc_assert (old_domain->NbColumns == new_domain->NbColumns
5563               && old_row < old_domain->NbRows
5564               && new_row < new_domain->NbRows);
5565
5566   for (i = 0; i < old_domain->NbColumns; i++)
5567     value_assign (new_domain->p[new_row][i], old_domain->p[old_row][i]);
5568 }
5569
5570 /* Swap coefficients of variables X and Y on row R.   */
5571
5572 static void
5573 swap_constraint_variables (CloogMatrix *domain,
5574                            int r, int x, int y)
5575 {
5576   value_swap (domain->p[r][x], domain->p[r][y]);
5577 }
5578
5579 /* Scale by X the coefficient C of constraint at row R in DOMAIN.  */
5580
5581 static void
5582 scale_constraint_variable (CloogMatrix *domain,
5583                            int r, int c, int x)
5584 {
5585   Value strip_size_value;
5586   value_init (strip_size_value);
5587   value_set_si (strip_size_value, x);
5588   value_multiply (domain->p[r][c], domain->p[r][c], strip_size_value);
5589   value_clear (strip_size_value);
5590 }
5591
5592 /* Strip mines the loop of BB at the position LOOP_DEPTH with STRIDE.
5593    Always valid, but not always a performance improvement.  */
5594   
5595 static void
5596 graphite_trans_bb_strip_mine (graphite_bb_p gb, int loop_depth, int stride)
5597 {
5598   int row, col;
5599
5600   CloogMatrix *domain = GBB_DOMAIN (gb);
5601   CloogMatrix *new_domain = cloog_matrix_alloc (domain->NbRows + 3,
5602                                                 domain->NbColumns + 1);   
5603
5604   int col_loop_old = loop_depth + 2; 
5605   int col_loop_strip = col_loop_old - 1;
5606
5607   gcc_assert (loop_depth <= gbb_nb_loops (gb) - 1);
5608
5609   VEC_safe_insert (loop_p, heap, GBB_LOOPS (gb), loop_depth, NULL);
5610
5611   GBB_DOMAIN (gb) = new_domain;
5612
5613   for (row = 0; row < domain->NbRows; row++)
5614     for (col = 0; col < domain->NbColumns; col++)
5615       if (col <= loop_depth)
5616         value_assign (new_domain->p[row][col], domain->p[row][col]);
5617       else
5618         value_assign (new_domain->p[row][col + 1], domain->p[row][col]);
5619
5620   row = domain->NbRows;
5621
5622   /* Lower bound of the outer stripped loop.  */
5623   copy_constraint (new_domain, new_domain,
5624                    get_lower_bound_row (new_domain, col_loop_old), row);
5625   swap_constraint_variables (new_domain, row, col_loop_old, col_loop_strip);
5626   row++;
5627
5628   /* Upper bound of the outer stripped loop.  */
5629   copy_constraint (new_domain, new_domain,
5630                    get_upper_bound_row (new_domain, col_loop_old), row);
5631   swap_constraint_variables (new_domain, row, col_loop_old, col_loop_strip);
5632   scale_constraint_variable (new_domain, row, col_loop_strip, stride);
5633   row++;
5634
5635   /* Lower bound of a tile starts at "stride * outer_iv".  */
5636   row = get_lower_bound_row (new_domain, col_loop_old);
5637   value_set_si (new_domain->p[row][0], 1);
5638   value_set_si (new_domain->p[row][const_column_index (new_domain)], 0);
5639   value_set_si (new_domain->p[row][col_loop_old], 1);
5640   value_set_si (new_domain->p[row][col_loop_strip], -1 * stride);
5641
5642   /* Upper bound of a tile stops at "stride * outer_iv + stride - 1",
5643      or at the old upper bound that is not modified.  */
5644   row = new_domain->NbRows - 1;
5645   value_set_si (new_domain->p[row][0], 1);
5646   value_set_si (new_domain->p[row][col_loop_old], -1);
5647   value_set_si (new_domain->p[row][col_loop_strip], stride);
5648   value_set_si (new_domain->p[row][const_column_index (new_domain)],
5649                 stride - 1);
5650
5651   cloog_matrix_free (domain);
5652
5653   /* Update static schedule.  */
5654   {
5655     int i;
5656     int nb_loops = gbb_nb_loops (gb);
5657     lambda_vector new_schedule = lambda_vector_new (nb_loops + 1);
5658
5659     for (i = 0; i <= loop_depth; i++)
5660       new_schedule[i] = GBB_STATIC_SCHEDULE (gb)[i];  
5661
5662     for (i = loop_depth + 1; i <= nb_loops - 2; i++)
5663       new_schedule[i + 2] = GBB_STATIC_SCHEDULE (gb)[i];  
5664
5665     GBB_STATIC_SCHEDULE (gb) = new_schedule;
5666   }
5667 }
5668
5669 /* Returns true when the strip mining of LOOP_INDEX by STRIDE is
5670    profitable or undecidable.  GB is the statement around which the
5671    loops will be strip mined.  */
5672
5673 static bool
5674 strip_mine_profitable_p (graphite_bb_p gb, int stride,
5675                          int loop_index)
5676 {
5677   bool res = true;
5678   edge exit = NULL;
5679   tree niter;
5680   loop_p loop;
5681   long niter_val;
5682
5683   loop = VEC_index (loop_p, GBB_LOOPS (gb), loop_index);
5684   exit = single_exit (loop);
5685
5686   niter = find_loop_niter (loop, &exit);
5687   if (niter == chrec_dont_know 
5688       || TREE_CODE (niter) != INTEGER_CST)
5689     return true;
5690   
5691   niter_val = int_cst_value (niter);
5692
5693   if (niter_val < stride)
5694     {
5695       res = false;
5696       if (dump_file && (dump_flags & TDF_DETAILS))
5697         {
5698           fprintf (dump_file, "\nStrip Mining is not profitable for loop %d:",
5699                    loop->num);
5700           fprintf (dump_file, "number of iterations is too low.\n");
5701         }
5702     }
5703   
5704   return res;
5705 }
5706  
5707 /* Determines when the interchange of LOOP_A and LOOP_B belonging to
5708    SCOP is legal.  DEPTH is the number of loops around.  */
5709
5710 static bool
5711 is_interchange_valid (scop_p scop, int loop_a, int loop_b, int depth)
5712 {
5713   bool res;
5714   VEC (ddr_p, heap) *dependence_relations;
5715   VEC (data_reference_p, heap) *datarefs;
5716
5717   struct loop *nest = VEC_index (loop_p, SCOP_LOOP_NEST (scop), loop_a);
5718   lambda_trans_matrix trans;
5719
5720   gcc_assert (loop_a < loop_b);
5721
5722   dependence_relations = VEC_alloc (ddr_p, heap, 10 * 10);
5723   datarefs = VEC_alloc (data_reference_p, heap, 10);
5724
5725   if (!compute_data_dependences_for_loop (nest, true, &datarefs,
5726                                           &dependence_relations))
5727     return false;
5728
5729   if (dump_file && (dump_flags & TDF_DETAILS))
5730     dump_ddrs (dump_file, dependence_relations);
5731
5732   trans = lambda_trans_matrix_new (depth, depth);
5733   lambda_matrix_id (LTM_MATRIX (trans), depth);
5734
5735   lambda_matrix_row_exchange (LTM_MATRIX (trans), 0, loop_b - loop_a);
5736
5737   if (!lambda_transform_legal_p (trans, depth, dependence_relations))
5738     {
5739       lambda_matrix_row_exchange (LTM_MATRIX (trans), 0, loop_b - loop_a);
5740       res = false;
5741     }
5742   else
5743     res = true;
5744
5745   free_dependence_relations (dependence_relations);
5746   free_data_refs (datarefs);
5747   return res;
5748 }
5749
5750 /* Loop block the LOOPS innermost loops of GB with stride size STRIDE. 
5751
5752    Example
5753
5754    for (i = 0; i <= 50; i++=4) 
5755      for (k = 0; k <= 100; k++=4) 
5756        for (l = 0; l <= 200; l++=4) 
5757          A
5758
5759    To strip mine the two inner most loops with stride = 4 call:
5760
5761    graphite_trans_bb_block (A, 4, 2) 
5762
5763    for (i = 0; i <= 50; i++) 
5764      for (kk = 0; kk <= 100; kk+=4) 
5765        for (ll = 0; ll <= 200; ll+=4) 
5766          for (k = kk; k <= min (100, kk + 3); k++) 
5767            for (l = ll; l <= min (200, ll + 3); l++) 
5768              A
5769 */
5770
5771 static bool
5772 graphite_trans_bb_block (graphite_bb_p gb, int stride, int loops)
5773 {
5774   int i, j;
5775   int nb_loops = gbb_nb_loops (gb);
5776   int start = nb_loops - loops;
5777   scop_p scop = GBB_SCOP (gb);
5778
5779   gcc_assert (scop_contains_loop (scop, gbb_loop (gb)));
5780
5781   for (i = start ; i < nb_loops; i++)
5782     for (j = i + 1; j < nb_loops; j++)
5783       if (!is_interchange_valid (scop, i, j, nb_loops))
5784         {
5785           if (dump_file && (dump_flags & TDF_DETAILS))
5786             fprintf (dump_file,
5787                      "\nInterchange not valid for loops %d and %d:\n", i, j);
5788           return false;
5789         }
5790       else if (dump_file && (dump_flags & TDF_DETAILS))
5791         fprintf (dump_file,
5792                  "\nInterchange valid for loops %d and %d:\n", i, j);
5793
5794   /* Check if strip mining is profitable for every loop.  */
5795   for (i = 0; i < nb_loops - start; i++)
5796     if (!strip_mine_profitable_p (gb, stride, start + i))
5797       return false;
5798
5799   /* Strip mine loops.  */
5800   for (i = 0; i < nb_loops - start; i++)
5801     graphite_trans_bb_strip_mine (gb, start + 2 * i, stride);
5802
5803   /* Interchange loops.  */
5804   for (i = 1; i < nb_loops - start; i++)
5805     graphite_trans_bb_move_loop (gb, start + 2 * i, start + i);
5806
5807   if (dump_file && (dump_flags & TDF_DETAILS))
5808     fprintf (dump_file, "\nLoops containing BB %d will be loop blocked.\n",
5809              GBB_BB (gb)->index);
5810
5811   return true;
5812 }
5813
5814 /* Loop block LOOPS innermost loops of a loop nest.  BBS represent the
5815    basic blocks that belong to the loop nest to be blocked.  */
5816
5817 static bool
5818 graphite_trans_loop_block (VEC (graphite_bb_p, heap) *bbs, int loops)
5819 {
5820   graphite_bb_p gb;
5821   int i;
5822   bool transform_done = false;
5823
5824   /* TODO: - Calculate the stride size automatically.  */
5825   int stride_size = 64;
5826
5827   for (i = 0; VEC_iterate (graphite_bb_p, bbs, i, gb); i++)
5828     transform_done |= graphite_trans_bb_block (gb, stride_size, loops);
5829
5830   return transform_done;
5831 }
5832
5833 /* Loop block all basic blocks of SCOP.  Return false when the
5834    transform is not performed.  */
5835
5836 static bool
5837 graphite_trans_scop_block (scop_p scop)
5838 {
5839   graphite_bb_p gb;
5840   int i, j;
5841   int last_nb_loops;
5842   int nb_loops;
5843   bool perfect = true;
5844   bool transform_done = false;
5845
5846   VEC (graphite_bb_p, heap) *bbs = VEC_alloc (graphite_bb_p, heap, 3);
5847   int max_schedule = scop_max_loop_depth (scop) + 1;
5848   lambda_vector last_schedule = lambda_vector_new (max_schedule);
5849
5850   if (VEC_length (graphite_bb_p, SCOP_BBS (scop)) == 0)
5851     return false;
5852
5853   /* Get the data of the first bb.  */
5854   gb = VEC_index (graphite_bb_p, SCOP_BBS (scop), 0);
5855   last_nb_loops = gbb_nb_loops (gb);
5856   lambda_vector_copy (GBB_STATIC_SCHEDULE (gb), last_schedule,
5857                       last_nb_loops + 1);
5858   VEC_safe_push (graphite_bb_p, heap, bbs, gb);
5859   
5860   for (i = 0; VEC_iterate (graphite_bb_p, SCOP_BBS (scop), i, gb); i++)
5861     {
5862       /* We did the first bb before.  */
5863       if (i == 0)
5864         continue;
5865
5866       nb_loops = gbb_nb_loops (gb);
5867
5868       /* If the number of loops is unchanged and only the last element of the
5869          schedule changes, we stay in the loop nest.  */
5870       if (nb_loops == last_nb_loops 
5871           && (last_schedule [nb_loops + 1]
5872               != GBB_STATIC_SCHEDULE (gb)[nb_loops + 1]))
5873         {
5874           VEC_safe_push (graphite_bb_p, heap, bbs, gb);
5875           continue;
5876         }
5877
5878       /* Otherwise, we left the innermost loop. So check, if the last bb was in
5879          a perfect loop nest and how many loops are contained in this perfect
5880          loop nest. 
5881          
5882          Count the number of zeros from the end of the schedule. They are the
5883          number of surrounding loops.
5884
5885          Example:
5886          last_bb  2 3 2 0 0 0 0 3
5887          bb       2 4 0
5888          <------  j = 4
5889         
5890          last_bb  2 3 2 0 0 0 0 3
5891          bb       2 3 2 0 1
5892          <--  j = 2
5893
5894          If there is no zero, there were other bbs in outer loops and the loop
5895          nest is not perfect.  */
5896       for (j = last_nb_loops - 1; j >= 0; j--)
5897         {
5898           if (last_schedule [j] != 0
5899               || (j <= nb_loops && GBB_STATIC_SCHEDULE (gb)[j] == 1))
5900             {
5901               j--;
5902               break;
5903             }
5904         }
5905       
5906       j++;
5907
5908       /* Found perfect loop nest.  */
5909       if (perfect && last_nb_loops - j >= 2)
5910         transform_done |= graphite_trans_loop_block (bbs, last_nb_loops - j);
5911  
5912       /* Check if we start with a new loop.
5913
5914          Example:
5915   
5916          last_bb  2 3 2 0 0 0 0 3
5917          bb       2 3 2 0 0 1 0
5918         
5919          Here we start with the loop "2 3 2 0 0 1" 
5920
5921          last_bb  2 3 2 0 0 0 0 3
5922          bb       2 3 2 0 0 1 
5923
5924          But here not, so the loop nest can never be perfect.  */
5925
5926       perfect = (GBB_STATIC_SCHEDULE (gb)[nb_loops] == 0);
5927
5928       /* Update the last_bb infos.  We do not do that for the bbs in the same
5929          loop, as the data we use is not changed.  */
5930       last_nb_loops = nb_loops;
5931       lambda_vector_copy (GBB_STATIC_SCHEDULE (gb), last_schedule,
5932                           nb_loops + 1);
5933       VEC_truncate (graphite_bb_p, bbs, 0);
5934       VEC_safe_push (graphite_bb_p, heap, bbs, gb);
5935     }
5936
5937   /* Check if the last loop nest was perfect.  It is the same check as above,
5938      but the comparison with the next bb is missing.  */
5939   for (j = last_nb_loops - 1; j >= 0; j--)
5940     if (last_schedule [j] != 0)
5941       {
5942         j--;
5943         break;
5944       }
5945
5946   j++;
5947
5948   /* Found perfect loop nest.  */
5949   if (last_nb_loops - j >= 2)
5950     transform_done |= graphite_trans_loop_block (bbs, last_nb_loops - j);
5951   VEC_free (graphite_bb_p, heap, bbs);
5952
5953   return transform_done;
5954 }
5955
5956 /* Apply graphite transformations to all the basic blocks of SCOP.  */
5957
5958 static bool
5959 graphite_apply_transformations (scop_p scop)
5960 {
5961   bool transform_done = false;
5962
5963   /* Sort the list of bbs.  Keep them always sorted.  */
5964   graphite_sort_gbbs (scop);
5965
5966   if (flag_loop_block)
5967     transform_done = graphite_trans_scop_block (scop);
5968
5969   /* Generate code even if we did not apply any real transformation.
5970      This also allows to check the performance for the identity
5971      transformation: GIMPLE -> GRAPHITE -> GIMPLE
5972      Keep in mind that CLooG optimizes in control, so the loop structure
5973      may change, even if we only use -fgraphite-identity.  */ 
5974   if (flag_graphite_identity)
5975     transform_done = true;
5976
5977   return transform_done;
5978 }
5979
5980 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop. 
5981
5982    Example:
5983
5984    for (i      |
5985      {         |
5986        for (j  |  SCoP 1
5987        for (k  |
5988      }         |
5989
5990    * SCoP frontier, as this line is not surrounded by any loop. *
5991
5992    for (l      |  SCoP 2
5993
5994    This is necessary as scalar evolution and parameter detection need a
5995    outermost loop to initialize parameters correctly.  
5996   
5997    TODO: FIX scalar evolution and parameter detection to allow more flexible
5998          SCoP frontiers.  */
5999
6000 static void
6001 limit_scops (void)
6002 {
6003   VEC (sd_region, heap) *tmp_scops = VEC_alloc (sd_region, heap, 3);
6004
6005   int i;
6006   scop_p scop;
6007
6008   for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
6009     {
6010       int j;
6011       loop_p loop;
6012       build_scop_bbs (scop);
6013
6014       if (!build_scop_loop_nests (scop))
6015         continue;
6016
6017       for (j = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), j, loop); j++) 
6018         if (!loop_in_scop_p (loop_outer (loop), scop))
6019           {
6020             sd_region open_scop;
6021             open_scop.entry = loop->header;
6022             open_scop.exit = single_exit (loop)->dest;
6023             VEC_safe_push (sd_region, heap, tmp_scops, &open_scop);
6024           }
6025     }
6026
6027   free_scops (current_scops);
6028   current_scops = VEC_alloc (scop_p, heap, 3);
6029
6030   create_sese_edges (tmp_scops);
6031   build_graphite_scops (tmp_scops);
6032   VEC_free (sd_region, heap, tmp_scops);
6033 }
6034
6035 /* Perform a set of linear transforms on the loops of the current
6036    function.  */
6037
6038 void
6039 graphite_transform_loops (void)
6040 {
6041   int i;
6042   scop_p scop;
6043
6044   if (number_of_loops () <= 1)
6045     return;
6046
6047   current_scops = VEC_alloc (scop_p, heap, 3);
6048   recompute_all_dominators ();
6049
6050   if (dump_file && (dump_flags & TDF_DETAILS))
6051     fprintf (dump_file, "Graphite loop transformations \n");
6052
6053   initialize_original_copy_tables ();
6054   cloog_initialize ();
6055   build_scops ();
6056   limit_scops ();
6057
6058   if (dump_file && (dump_flags & TDF_DETAILS))
6059     fprintf (dump_file, "\nnumber of SCoPs: %d\n",
6060              VEC_length (scop_p, current_scops));
6061
6062   for (i = 0; VEC_iterate (scop_p, current_scops, i, scop); i++)
6063     {
6064       build_scop_bbs (scop);
6065       if (!build_scop_loop_nests (scop))
6066         continue;
6067
6068       build_bb_loops (scop);
6069
6070       if (!build_scop_conditions (scop))
6071         continue;
6072
6073       find_scop_parameters (scop);
6074       build_scop_context (scop);
6075
6076       if (dump_file && (dump_flags & TDF_DETAILS))
6077         {
6078           fprintf (dump_file, "\n(In SCoP %d:\n", i);
6079           fprintf (dump_file, "\nnumber of bbs: %d\n",
6080                    VEC_length (graphite_bb_p, SCOP_BBS (scop)));
6081           fprintf (dump_file, "\nnumber of loops: %d)\n",
6082                    VEC_length (loop_p, SCOP_LOOP_NEST (scop)));
6083         }
6084
6085       if (!build_scop_iteration_domain (scop))
6086         continue;
6087
6088       add_conditions_to_constraints (scop);
6089       build_scop_canonical_schedules (scop);
6090
6091       build_scop_data_accesses (scop);
6092       build_scop_dynamic_schedules (scop);
6093
6094       if (dump_file && (dump_flags & TDF_DETAILS))
6095         {
6096           int nbrefs = nb_data_refs_in_scop (scop);
6097           fprintf (dump_file, "\nnumber of data refs: %d\n", nbrefs);
6098         }
6099
6100       if (graphite_apply_transformations (scop))
6101         gloog (scop, find_transform (scop));
6102 #ifdef ENABLE_CHECKING
6103       else
6104         {
6105           struct clast_stmt *stmt = find_transform (scop);
6106           cloog_clast_free (stmt);
6107         }
6108 #endif
6109     }
6110
6111   /* Cleanup.  */
6112   cleanup_tree_cfg ();
6113   free_scops (current_scops);
6114   cloog_finalize ();
6115   free_original_copy_tables ();
6116 }
6117
6118 #else /* If Cloog is not available: #ifndef HAVE_cloog.  */
6119
6120 void
6121 graphite_transform_loops (void)
6122 {
6123   sorry ("Graphite loop optimizations cannot be used");
6124 }
6125
6126 #endif