OSDN Git Service

* config/bfin/bfin.h (LINK_SPEC): Remove %{YP,*}.
[pf3gnuchains/gcc-fork.git] / gcc / graphite-clast-to-gimple.c
1 /* Translation of CLAST (CLooG AST) to Gimple.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3    Contributed by Sebastian Pop <sebastian.pop@amd.com>.
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "tree-flow.h"
26 #include "tree-dump.h"
27 #include "cfgloop.h"
28 #include "tree-chrec.h"
29 #include "tree-data-ref.h"
30 #include "tree-scalar-evolution.h"
31 #include "sese.h"
32
33 #ifdef HAVE_cloog
34 #include "cloog/cloog.h"
35 #include "ppl_c.h"
36 #include "graphite-cloog-util.h"
37 #include "graphite-ppl.h"
38 #include "graphite-poly.h"
39 #include "graphite-clast-to-gimple.h"
40 #include "graphite-dependences.h"
41 #include "graphite-cloog-compat.h"
42
43 /* This flag is set when an error occurred during the translation of
44    CLAST to Gimple.  */
45 static bool gloog_error;
46
47 /* Verifies properties that GRAPHITE should maintain during translation.  */
48
49 static inline void
50 graphite_verify (void)
51 {
52 #ifdef ENABLE_CHECKING
53   verify_loop_structure ();
54   verify_dominators (CDI_DOMINATORS);
55   verify_loop_closed_ssa (true);
56 #endif
57 }
58
59 /* Stores the INDEX in a vector for a given clast NAME.  */
60
61 typedef struct clast_name_index {
62   int index;
63   const char *name;
64 } *clast_name_index_p;
65
66 /* Returns a pointer to a new element of type clast_name_index_p built
67    from NAME and INDEX.  */
68
69 static inline clast_name_index_p
70 new_clast_name_index (const char *name, int index)
71 {
72   clast_name_index_p res = XNEW (struct clast_name_index);
73
74   res->name = name;
75   res->index = index;
76   return res;
77 }
78
79 /* For a given clast NAME, returns -1 if it does not correspond to any
80    parameter, or otherwise, returns the index in the PARAMS or
81    SCATTERING_DIMENSIONS vector.  */
82
83 static inline int
84 clast_name_to_index (clast_name_p name, htab_t index_table)
85 {
86   struct clast_name_index tmp;
87   PTR *slot;
88
89 #ifdef CLOOG_ORG
90   gcc_assert (name->type == clast_expr_name);
91   tmp.name = ((const struct clast_name*) name)->name;
92 #else
93   tmp.name = name;
94 #endif
95
96   slot = htab_find_slot (index_table, &tmp, NO_INSERT);
97
98   if (slot && *slot)
99     return ((struct clast_name_index *) *slot)->index;
100
101   return -1;
102 }
103
104 /* Records in INDEX_TABLE the INDEX for NAME.  */
105
106 static inline void
107 save_clast_name_index (htab_t index_table, const char *name, int index)
108 {
109   struct clast_name_index tmp;
110   PTR *slot;
111
112   tmp.name = name;
113   slot = htab_find_slot (index_table, &tmp, INSERT);
114
115   if (slot)
116     {
117       if (*slot)
118         free (*slot);
119
120       *slot = new_clast_name_index (name, index);
121     }
122 }
123
124 /* Computes a hash function for database element ELT.  */
125
126 static inline hashval_t
127 clast_name_index_elt_info (const void *elt)
128 {
129   return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
130 }
131
132 /* Compares database elements E1 and E2.  */
133
134 static inline int
135 eq_clast_name_indexes (const void *e1, const void *e2)
136 {
137   const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
138   const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
139
140   return (elt1->name == elt2->name);
141 }
142
143 /* For a given scattering dimension, return the new induction variable
144    associated to it.  */
145
146 static inline tree
147 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
148 {
149   return VEC_index (tree, newivs, depth);
150 }
151
152 \f
153
154 /* Returns the tree variable from the name NAME that was given in
155    Cloog representation.  */
156
157 static tree
158 clast_name_to_gcc (clast_name_p name, sese region, VEC (tree, heap) *newivs,
159                    htab_t newivs_index, htab_t params_index)
160 {
161   int index;
162   VEC (tree, heap) *params = SESE_PARAMS (region);
163
164   if (params && params_index)
165     {
166       index = clast_name_to_index (name, params_index);
167
168       if (index >= 0)
169         return VEC_index (tree, params, index);
170     }
171
172   gcc_assert (newivs && newivs_index);
173   index = clast_name_to_index (name, newivs_index);
174   gcc_assert (index >= 0);
175
176   return newivs_to_depth_to_newiv (newivs, index);
177 }
178
179 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2.  */
180
181 static tree
182 max_signed_precision_type (tree type1, tree type2)
183 {
184   int p1 = TYPE_PRECISION (type1);
185   int p2 = TYPE_PRECISION (type2);
186   int precision;
187   tree type;
188   enum machine_mode mode;
189
190   if (p1 > p2)
191     precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
192   else
193     precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
194
195   if (precision > BITS_PER_WORD)
196     {
197       gloog_error = true;
198       return integer_type_node;
199     }
200
201   mode = smallest_mode_for_size (precision, MODE_INT);
202   precision = GET_MODE_PRECISION (mode);
203   type = build_nonstandard_integer_type (precision, false);
204
205   if (!type)
206     {
207       gloog_error = true;
208       return integer_type_node;
209     }
210
211   return type;
212 }
213
214 /* Returns the maximal precision type for expressions TYPE1 and TYPE2.  */
215
216 static tree
217 max_precision_type (tree type1, tree type2)
218 {
219   if (POINTER_TYPE_P (type1))
220     return type1;
221
222   if (POINTER_TYPE_P (type2))
223     return type2;
224
225   if (!TYPE_UNSIGNED (type1)
226       || !TYPE_UNSIGNED (type2))
227     return max_signed_precision_type (type1, type2);
228
229   return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
230 }
231
232 static tree
233 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
234                          htab_t, htab_t);
235
236 /* Converts a Cloog reduction expression R with reduction operation OP
237    to a GCC expression tree of type TYPE.  */
238
239 static tree
240 clast_to_gcc_expression_red (tree type, enum tree_code op,
241                              struct clast_reduction *r,
242                              sese region, VEC (tree, heap) *newivs,
243                              htab_t newivs_index, htab_t params_index)
244 {
245   int i;
246   tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
247                                       newivs_index, params_index);
248   tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
249
250   for (i = 1; i < r->n; i++)
251     {
252       tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
253                                         newivs, newivs_index, params_index);
254       res = fold_build2 (op, type, res, t);
255     }
256
257   return res;
258 }
259
260 /* Converts a Cloog AST expression E back to a GCC expression tree of
261    type TYPE.  */
262
263 static tree
264 clast_to_gcc_expression (tree type, struct clast_expr *e,
265                          sese region, VEC (tree, heap) *newivs,
266                          htab_t newivs_index, htab_t params_index)
267 {
268   switch (e->type)
269     {
270     case clast_expr_term:
271       {
272         struct clast_term *t = (struct clast_term *) e;
273
274         if (t->var)
275           {
276             if (mpz_cmp_si (t->val, 1) == 0)
277               {
278                 tree name = clast_name_to_gcc (t->var, region, newivs,
279                                                newivs_index, params_index);
280
281                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
282                   name = fold_convert (sizetype, name);
283
284                 name = fold_convert (type, name);
285                 return name;
286               }
287
288             else if (mpz_cmp_si (t->val, -1) == 0)
289               {
290                 tree name = clast_name_to_gcc (t->var, region, newivs,
291                                                newivs_index, params_index);
292
293                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
294                   name = fold_convert (sizetype, name);
295
296                 name = fold_convert (type, name);
297
298                 return fold_build1 (NEGATE_EXPR, type, name);
299               }
300             else
301               {
302                 tree name = clast_name_to_gcc (t->var, region, newivs,
303                                                newivs_index, params_index);
304                 tree cst = gmp_cst_to_tree (type, t->val);
305
306                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
307                   name = fold_convert (sizetype, name);
308
309                 name = fold_convert (type, name);
310
311                 if (!POINTER_TYPE_P (type))
312                   return fold_build2 (MULT_EXPR, type, cst, name);
313
314                 gloog_error = true;
315                 return cst;
316               }
317           }
318         else
319           return gmp_cst_to_tree (type, t->val);
320       }
321
322     case clast_expr_red:
323       {
324         struct clast_reduction *r = (struct clast_reduction *) e;
325
326         switch (r->type)
327           {
328           case clast_red_sum:
329             return clast_to_gcc_expression_red
330               (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
331                r, region, newivs, newivs_index, params_index);
332
333           case clast_red_min:
334             return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
335                                                 newivs, newivs_index,
336                                                 params_index);
337
338           case clast_red_max:
339             return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
340                                                 newivs, newivs_index,
341                                                 params_index);
342
343           default:
344             gcc_unreachable ();
345           }
346         break;
347       }
348
349     case clast_expr_bin:
350       {
351         struct clast_binary *b = (struct clast_binary *) e;
352         struct clast_expr *lhs = (struct clast_expr *) b->LHS;
353         tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
354                                            newivs_index, params_index);
355         tree tr = gmp_cst_to_tree (type, b->RHS);
356
357         switch (b->type)
358           {
359           case clast_bin_fdiv:
360             return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
361
362           case clast_bin_cdiv:
363             return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
364
365           case clast_bin_div:
366             return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
367
368           case clast_bin_mod:
369             return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
370
371           default:
372             gcc_unreachable ();
373           }
374       }
375
376     default:
377       gcc_unreachable ();
378     }
379
380   return NULL_TREE;
381 }
382
383 /* Return the precision needed to represent the value VAL.  */
384
385 static int
386 precision_for_value (mpz_t val)
387 {
388   mpz_t x, y, two;
389   int precision;
390
391   mpz_init (x);
392   mpz_init (y);
393   mpz_init (two);
394   mpz_set_si (x, 2);
395   mpz_set (y, val);
396   mpz_set_si (two, 2);
397   precision = 1;
398
399   if (mpz_sgn (y) < 0)
400     mpz_neg (y, y);
401
402   while (mpz_cmp (y, x) >= 0)
403     {
404       mpz_mul (x, x, two);
405       precision++;
406     }
407
408   mpz_clear (x);
409   mpz_clear (y);
410   mpz_clear (two);
411
412   return precision;
413 }
414
415 /* Return the precision needed to represent the values between LOW and
416    UP.  */
417
418 static int
419 precision_for_interval (mpz_t low, mpz_t up)
420 {
421   mpz_t diff;
422   int precision;
423
424   gcc_assert (mpz_cmp (low, up) <= 0);
425
426   mpz_init (diff);
427   mpz_sub (diff, up, low);
428   precision = precision_for_value (diff);
429   mpz_clear (diff);
430
431   return precision;
432 }
433
434 /* Return a type that could represent the integer value VAL.  */
435
436 static tree
437 gcc_type_for_interval (mpz_t low, mpz_t up)
438 {
439   bool unsigned_p = true;
440   int precision, prec_up, prec_int;
441   tree type;
442   enum machine_mode mode;
443
444   gcc_assert (mpz_cmp (low, up) <= 0);
445
446   prec_up = precision_for_value (up);
447   prec_int = precision_for_interval (low, up);
448   precision = MAX (prec_up, prec_int);
449
450   if (precision > BITS_PER_WORD)
451     {
452       gloog_error = true;
453       return integer_type_node;
454     }
455
456   if (mpz_sgn (low) <= 0)
457     unsigned_p = false;
458
459   else if (precision < BITS_PER_WORD)
460     {
461       unsigned_p = false;
462       precision++;
463     }
464
465   mode = smallest_mode_for_size (precision, MODE_INT);
466   precision = GET_MODE_PRECISION (mode);
467   type = build_nonstandard_integer_type (precision, unsigned_p);
468
469   if (!type)
470     {
471       gloog_error = true;
472       return integer_type_node;
473     }
474
475   return type;
476 }
477
478 /* Return a type that could represent the integer value VAL, or
479    otherwise return NULL_TREE.  */
480
481 static tree
482 gcc_type_for_value (mpz_t val)
483 {
484   return gcc_type_for_interval (val, val);
485 }
486
487 /* Return the type for the clast_term T used in STMT.  */
488
489 static tree
490 gcc_type_for_clast_term (struct clast_term *t,
491                          sese region, VEC (tree, heap) *newivs,
492                          htab_t newivs_index, htab_t params_index)
493 {
494   gcc_assert (t->expr.type == clast_expr_term);
495
496   if (!t->var)
497     return gcc_type_for_value (t->val);
498
499   return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
500                                        newivs_index, params_index));
501 }
502
503 static tree
504 gcc_type_for_clast_expr (struct clast_expr *, sese,
505                          VEC (tree, heap) *, htab_t, htab_t);
506
507 /* Return the type for the clast_reduction R used in STMT.  */
508
509 static tree
510 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
511                         VEC (tree, heap) *newivs,
512                         htab_t newivs_index, htab_t params_index)
513 {
514   int i;
515   tree type = NULL_TREE;
516
517   if (r->n == 1)
518     return gcc_type_for_clast_expr (r->elts[0], region, newivs,
519                                     newivs_index, params_index);
520
521   switch (r->type)
522     {
523     case clast_red_sum:
524     case clast_red_min:
525     case clast_red_max:
526       type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
527                                       newivs_index, params_index);
528       for (i = 1; i < r->n; i++)
529         type = max_precision_type (type, gcc_type_for_clast_expr
530                                    (r->elts[i], region, newivs,
531                                     newivs_index, params_index));
532
533       return type;
534
535     default:
536       break;
537     }
538
539   gcc_unreachable ();
540   return NULL_TREE;
541 }
542
543 /* Return the type for the clast_binary B used in STMT.  */
544
545 static tree
546 gcc_type_for_clast_bin (struct clast_binary *b,
547                         sese region, VEC (tree, heap) *newivs,
548                         htab_t newivs_index, htab_t params_index)
549 {
550   tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
551                                     newivs, newivs_index, params_index);
552   tree r = gcc_type_for_value (b->RHS);
553   return max_signed_precision_type (l, r);
554 }
555
556 /* Returns the type for the CLAST expression E when used in statement
557    STMT.  */
558
559 static tree
560 gcc_type_for_clast_expr (struct clast_expr *e,
561                          sese region, VEC (tree, heap) *newivs,
562                          htab_t newivs_index, htab_t params_index)
563 {
564   switch (e->type)
565     {
566     case clast_expr_term:
567       return gcc_type_for_clast_term ((struct clast_term *) e, region,
568                                       newivs, newivs_index, params_index);
569
570     case clast_expr_red:
571       return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
572                                      newivs, newivs_index, params_index);
573
574     case clast_expr_bin:
575       return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
576                                      newivs, newivs_index, params_index);
577
578     default:
579       gcc_unreachable ();
580     }
581
582   return NULL_TREE;
583 }
584
585 /* Returns the type for the equation CLEQ.  */
586
587 static tree
588 gcc_type_for_clast_eq (struct clast_equation *cleq,
589                        sese region, VEC (tree, heap) *newivs,
590                        htab_t newivs_index, htab_t params_index)
591 {
592   tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
593                                     newivs_index, params_index);
594   tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
595                                     newivs_index, params_index);
596   return max_precision_type (l, r);
597 }
598
599 /* Translates a clast equation CLEQ to a tree.  */
600
601 static tree
602 graphite_translate_clast_equation (sese region,
603                                    struct clast_equation *cleq,
604                                    VEC (tree, heap) *newivs,
605                                    htab_t newivs_index, htab_t params_index)
606 {
607   enum tree_code comp;
608   tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
609                                      params_index);
610   tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
611                                       newivs_index, params_index);
612   tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
613                                       newivs_index, params_index);
614
615   if (cleq->sign == 0)
616     comp = EQ_EXPR;
617
618   else if (cleq->sign > 0)
619     comp = GE_EXPR;
620
621   else
622     comp = LE_EXPR;
623
624   return fold_build2 (comp, boolean_type_node, lhs, rhs);
625 }
626
627 /* Creates the test for the condition in STMT.  */
628
629 static tree
630 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
631                                  VEC (tree, heap) *newivs,
632                                  htab_t newivs_index, htab_t params_index)
633 {
634   tree cond = NULL;
635   int i;
636
637   for (i = 0; i < stmt->n; i++)
638     {
639       tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
640                                                    newivs, newivs_index,
641                                                    params_index);
642
643       if (cond)
644         cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
645       else
646         cond = eq;
647     }
648
649   return cond;
650 }
651
652 /* Creates a new if region corresponding to Cloog's guard.  */
653
654 static edge
655 graphite_create_new_guard (sese region, edge entry_edge,
656                            struct clast_guard *stmt,
657                            VEC (tree, heap) *newivs,
658                            htab_t newivs_index, htab_t params_index)
659 {
660   tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
661                                                     newivs_index, params_index);
662   edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
663   return exit_edge;
664 }
665
666 /* Compute the lower bound LOW and upper bound UP for the induction
667    variable at LEVEL for the statement PBB, based on the transformed
668    scattering of PBB: T|I|G|Cst, with T the scattering transform, I
669    the iteration domain, and G the context parameters.  */
670
671 static void
672 compute_bounds_for_level (poly_bb_p pbb, int level, mpz_t low, mpz_t up)
673 {
674   ppl_Pointset_Powerset_C_Polyhedron_t ps;
675   ppl_Linear_Expression_t le;
676
677   combine_context_id_scat (&ps, pbb, false);
678
679   /* Prepare the linear expression corresponding to the level that we
680      want to maximize/minimize.  */
681   {
682     ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
683       + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
684
685     ppl_new_Linear_Expression_with_dimension (&le, dim);
686     ppl_set_coef (le, 2 * level + 1, 1);
687   }
688
689   ppl_max_for_le_pointset (ps, le, up);
690   ppl_min_for_le_pointset (ps, le, low);
691   ppl_delete_Linear_Expression (le);
692   ppl_delete_Pointset_Powerset_C_Polyhedron (ps);
693 }
694
695 /* Compute the type for the induction variable at LEVEL for the
696    statement PBB, based on the transformed schedule of PBB.  */
697
698 static tree
699 compute_type_for_level (poly_bb_p pbb, int level)
700 {
701   mpz_t low, up;
702   tree type;
703
704   mpz_init (low);
705   mpz_init (up);
706
707   compute_bounds_for_level (pbb, level, low, up);
708   type = gcc_type_for_interval (low, up);
709
710   mpz_clear (low);
711   mpz_clear (up);
712   return type;
713 }
714
715 /* Walks a CLAST and returns the first statement in the body of a
716    loop.  */
717
718 static struct clast_user_stmt *
719 clast_get_body_of_loop (struct clast_stmt *stmt)
720 {
721   if (!stmt
722       || CLAST_STMT_IS_A (stmt, stmt_user))
723     return (struct clast_user_stmt *) stmt;
724
725   if (CLAST_STMT_IS_A (stmt, stmt_for))
726     return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
727
728   if (CLAST_STMT_IS_A (stmt, stmt_guard))
729     return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
730
731   if (CLAST_STMT_IS_A (stmt, stmt_block))
732     return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
733
734   gcc_unreachable ();
735 }
736
737 /* Returns the type for the induction variable for the loop translated
738    from STMT_FOR.  */
739
740 static tree
741 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
742                                tree lb_type, tree ub_type)
743 {
744   struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
745   struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
746   CloogStatement *cs = body->statement;
747   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
748
749   return max_signed_precision_type (lb_type, max_precision_type
750                                     (ub_type, compute_type_for_level
751                                      (pbb, level - 1)));
752 }
753
754 /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an
755    induction variable for the new LOOP.  New LOOP is attached to CFG
756    starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
757    becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
758    CLooG's scattering name to the induction variable created for the
759    loop of STMT.  The new induction variable is inserted in the NEWIVS
760    vector.  */
761
762 static struct loop *
763 graphite_create_new_loop (sese region, edge entry_edge,
764                           struct clast_for *stmt,
765                           loop_p outer, VEC (tree, heap) **newivs,
766                           htab_t newivs_index, htab_t params_index, int level)
767 {
768   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
769                                           newivs_index, params_index);
770   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
771                                           newivs_index, params_index);
772   tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
773   tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
774                                      newivs_index, params_index);
775   tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
776                                      newivs_index, params_index);
777   tree stride = gmp_cst_to_tree (type, stmt->stride);
778   tree ivvar = create_tmp_var (type, "graphite_IV");
779   tree iv, iv_after_increment;
780   loop_p loop = create_empty_loop_on_edge
781     (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
782      outer ? outer : entry_edge->src->loop_father);
783
784   add_referenced_var (ivvar);
785
786   save_clast_name_index (newivs_index, stmt->iterator,
787                          VEC_length (tree, *newivs));
788   VEC_safe_push (tree, heap, *newivs, iv);
789   return loop;
790 }
791
792 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
793    induction variables of the loops around GBB in SESE.  */
794
795 static void
796 build_iv_mapping (VEC (tree, heap) *iv_map, sese region,
797                   VEC (tree, heap) *newivs, htab_t newivs_index,
798                   struct clast_user_stmt *user_stmt,
799                   htab_t params_index)
800 {
801   struct clast_stmt *t;
802   int depth = 0;
803   CloogStatement *cs = user_stmt->statement;
804   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
805   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
806
807   for (t = user_stmt->substitutions; t; t = t->next, depth++)
808     {
809       struct clast_expr *expr = (struct clast_expr *)
810        ((struct clast_assignment *)t)->RHS;
811       tree type = gcc_type_for_clast_expr (expr, region, newivs,
812                                            newivs_index, params_index);
813       tree new_name = clast_to_gcc_expression (type, expr, region, newivs,
814                                                newivs_index, params_index);
815       loop_p old_loop = gbb_loop_at_index (gbb, region, depth);
816
817       VEC_replace (tree, iv_map, old_loop->num, new_name);
818     }
819 }
820
821 /* Construct bb_pbb_def with BB and PBB.  */
822
823 static bb_pbb_def *
824 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
825 {
826   bb_pbb_def *bb_pbb_p;
827
828   bb_pbb_p = XNEW (bb_pbb_def);
829   bb_pbb_p->bb = bb;
830   bb_pbb_p->pbb = pbb;
831
832   return bb_pbb_p;
833 }
834
835 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING.  */
836
837 static void
838 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
839 {
840   bb_pbb_def tmp;
841   PTR *x;
842
843   tmp.bb = bb;
844   x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
845
846   if (x && !*x)
847     *x = new_bb_pbb_def (bb, pbb);
848 }
849
850 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING.  */
851
852 static poly_bb_p
853 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
854 {
855   bb_pbb_def tmp;
856   PTR *slot;
857
858   tmp.bb = bb;
859   slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
860
861   if (slot && *slot)
862     return ((bb_pbb_def *) *slot)->pbb;
863
864   return NULL;
865 }
866
867 /* Check data dependency in LOOP at scattering level LEVEL.
868    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
869    mapping.  */
870
871 static bool
872 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
873 {
874   unsigned i,j;
875   basic_block *bbs = get_loop_body_in_dom_order (loop);
876
877   for (i = 0; i < loop->num_nodes; i++)
878     {
879       poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
880
881       if (pbb1 == NULL)
882        continue;
883
884       for (j = 0; j < loop->num_nodes; j++)
885        {
886          poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
887
888          if (pbb2 == NULL)
889            continue;
890
891          if (dependency_between_pbbs_p (pbb1, pbb2, level))
892            {
893              free (bbs);
894              return true;
895            }
896        }
897     }
898
899   free (bbs);
900
901   return false;
902 }
903
904 /* Translates a clast user statement STMT to gimple.
905
906    - REGION is the sese region we used to generate the scop.
907    - NEXT_E is the edge where new generated code should be attached.
908    - CONTEXT_LOOP is the loop in which the generated code will be placed
909    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
910    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
911      the sese region.  */
912 static edge
913 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
914                       VEC (tree, heap) **newivs,
915                       htab_t newivs_index, htab_t bb_pbb_mapping,
916                       htab_t params_index)
917 {
918   int i, nb_loops;
919   basic_block new_bb;
920   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
921   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
922   VEC (tree, heap) *iv_map;
923
924   if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
925     return next_e;
926
927   nb_loops = number_of_loops ();
928   iv_map = VEC_alloc (tree, heap, nb_loops);
929   for (i = 0; i < nb_loops; i++)
930     VEC_quick_push (tree, iv_map, NULL_TREE);
931
932   build_iv_mapping (iv_map, region, *newivs, newivs_index, stmt, params_index);
933   next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
934                                            next_e, iv_map);
935   VEC_free (tree, heap, iv_map);
936
937   new_bb = next_e->src;
938   mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
939   update_ssa (TODO_update_ssa);
940
941   return next_e;
942 }
943
944 /* Creates a new if region protecting the loop to be executed, if the execution
945    count is zero (lb > ub).  */
946
947 static edge
948 graphite_create_new_loop_guard (sese region, edge entry_edge,
949                                 struct clast_for *stmt,
950                                 VEC (tree, heap) *newivs,
951                                 htab_t newivs_index, htab_t params_index)
952 {
953   tree cond_expr;
954   edge exit_edge;
955   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
956                                           newivs_index, params_index);
957   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
958                                           newivs_index, params_index);
959   tree type = max_precision_type (lb_type, ub_type);
960   tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
961                                      newivs_index, params_index);
962   tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
963                                      newivs_index, params_index);
964   /* When ub is simply a constant or a parameter, use lb <= ub.  */
965   if (TREE_CODE (ub) == INTEGER_CST || TREE_CODE (ub) == SSA_NAME)
966     cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
967   else
968     {
969       tree one = (POINTER_TYPE_P (type)
970                   ? size_one_node
971                   : fold_convert (type, integer_one_node));
972       /* Adding +1 and using LT_EXPR helps with loop latches that have a
973          loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
974          2^k-1 due to integer overflow, and the condition lb <= ub is true,
975          even if we do not want this.  However lb < ub + 1 is false, as
976          expected.  */
977       tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
978                                  : PLUS_EXPR, type, ub, one);
979
980       cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
981     }
982
983   exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
984
985   return exit_edge;
986 }
987
988 static edge
989 translate_clast (sese, loop_p, struct clast_stmt *, edge,
990                  VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
991
992 /* Create the loop for a clast for statement.
993
994    - REGION is the sese region we used to generate the scop.
995    - NEXT_E is the edge where new generated code should be attached.
996    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
997    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
998      the sese region.  */
999 static edge
1000 translate_clast_for_loop (sese region, loop_p context_loop,
1001                           struct clast_for *stmt, edge next_e,
1002                           VEC (tree, heap) **newivs,
1003                           htab_t newivs_index, htab_t bb_pbb_mapping,
1004                           int level, htab_t params_index)
1005 {
1006   struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1007                                                 context_loop, newivs,
1008                                                 newivs_index, params_index,
1009                                                 level);
1010   edge last_e = single_exit (loop);
1011   edge to_body = single_succ_edge (loop->header);
1012   basic_block after = to_body->dest;
1013
1014   /* Create a basic block for loop close phi nodes.  */
1015   last_e = single_succ_edge (split_edge (last_e));
1016
1017   /* Translate the body of the loop.  */
1018   next_e = translate_clast (region, loop, stmt->body, to_body,
1019                             newivs, newivs_index, bb_pbb_mapping, level + 1,
1020                             params_index);
1021   redirect_edge_succ_nodup (next_e, after);
1022   set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1023
1024   if (flag_loop_parallelize_all
1025       && !dependency_in_loop_p (loop, bb_pbb_mapping,
1026                                 get_scattering_level (level)))
1027     loop->can_be_parallel = true;
1028
1029   return last_e;
1030 }
1031
1032 /* Translates a clast for statement STMT to gimple.  First a guard is created
1033    protecting the loop, if it is executed zero times.  In this guard we create
1034    the real loop structure.
1035
1036    - REGION is the sese region we used to generate the scop.
1037    - NEXT_E is the edge where new generated code should be attached.
1038    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1039    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1040      the sese region.  */
1041 static edge
1042 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1043                      edge next_e, VEC (tree, heap) **newivs,
1044                      htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1045                      htab_t params_index)
1046 {
1047   edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1048                                                 newivs_index, params_index);
1049   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1050
1051   translate_clast_for_loop (region, context_loop, stmt, true_e, newivs,
1052                             newivs_index, bb_pbb_mapping, level,
1053                             params_index);
1054   return last_e;
1055 }
1056
1057 /* Translates a clast guard statement STMT to gimple.
1058
1059    - REGION is the sese region we used to generate the scop.
1060    - NEXT_E is the edge where new generated code should be attached.
1061    - CONTEXT_LOOP is the loop in which the generated code will be placed
1062    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1063    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1064      the sese region.  */
1065 static edge
1066 translate_clast_guard (sese region, loop_p context_loop,
1067                        struct clast_guard *stmt, edge next_e,
1068                        VEC (tree, heap) **newivs,
1069                        htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1070                        htab_t params_index)
1071 {
1072   edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1073                                            newivs_index, params_index);
1074   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1075
1076   translate_clast (region, context_loop, stmt->then, true_e,
1077                    newivs, newivs_index, bb_pbb_mapping,
1078                    level, params_index);
1079   return last_e;
1080 }
1081
1082 /* Translates a CLAST statement STMT to GCC representation in the
1083    context of a SESE.
1084
1085    - NEXT_E is the edge where new generated code should be attached.
1086    - CONTEXT_LOOP is the loop in which the generated code will be placed
1087    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1088 static edge
1089 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1090                  edge next_e, VEC (tree, heap) **newivs,
1091                  htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1092                  htab_t params_index)
1093 {
1094   if (!stmt)
1095     return next_e;
1096
1097   if (CLAST_STMT_IS_A (stmt, stmt_root))
1098     ; /* Do nothing.  */
1099
1100   else if (CLAST_STMT_IS_A (stmt, stmt_user))
1101     next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1102                                    next_e, newivs, newivs_index,
1103                                    bb_pbb_mapping, params_index);
1104
1105   else if (CLAST_STMT_IS_A (stmt, stmt_for))
1106     next_e = translate_clast_for (region, context_loop,
1107                                   (struct clast_for *) stmt, next_e,
1108                                   newivs, newivs_index,
1109                                   bb_pbb_mapping, level, params_index);
1110
1111   else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1112     next_e = translate_clast_guard (region, context_loop,
1113                                     (struct clast_guard *) stmt, next_e,
1114                                     newivs, newivs_index,
1115                                     bb_pbb_mapping, level, params_index);
1116
1117   else if (CLAST_STMT_IS_A (stmt, stmt_block))
1118     next_e = translate_clast (region, context_loop,
1119                               ((struct clast_block *) stmt)->body,
1120                               next_e, newivs, newivs_index,
1121                               bb_pbb_mapping, level, params_index);
1122   else
1123     gcc_unreachable();
1124
1125   recompute_all_dominators ();
1126   graphite_verify ();
1127
1128   return translate_clast (region, context_loop, stmt->next, next_e,
1129                           newivs, newivs_index,
1130                           bb_pbb_mapping, level, params_index);
1131 }
1132
1133 /* Free the SCATTERING domain list.  */
1134
1135 static void
1136 free_scattering (CloogScatteringList *scattering)
1137 {
1138   while (scattering)
1139     {
1140       CloogScattering *dom = cloog_scattering (scattering);
1141       CloogScatteringList *next = cloog_next_scattering (scattering);
1142
1143       cloog_scattering_free (dom);
1144       free (scattering);
1145       scattering = next;
1146     }
1147 }
1148
1149 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1150    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1151    from 0 to scop_nb_loops (scop).  */
1152
1153 static void
1154 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1155 {
1156   sese region = SCOP_REGION (scop);
1157   int i;
1158   int nb_iterators = scop_max_loop_depth (scop);
1159   int nb_scattering = cloog_program_nb_scattdims (prog);
1160   int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1161   char **iterators = XNEWVEC (char *, nb_iterators * 2);
1162   char **scattering = XNEWVEC (char *, nb_scattering);
1163   char **parameters= XNEWVEC (char *, nb_parameters);
1164
1165   cloog_program_set_names (prog, cloog_names_malloc ());
1166
1167   for (i = 0; i < nb_parameters; i++)
1168     {
1169       tree param = VEC_index (tree, SESE_PARAMS(region), i);
1170       const char *name = get_name (param);
1171       int len;
1172
1173       if (!name)
1174         name = "T";
1175
1176       len = strlen (name);
1177       len += 17;
1178       parameters[i] = XNEWVEC (char, len + 1);
1179       snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1180     }
1181
1182   cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1183   cloog_names_set_parameters (cloog_program_names (prog), parameters);
1184
1185   for (i = 0; i < nb_iterators; i++)
1186     {
1187       int len = 4 + 16;
1188       iterators[i] = XNEWVEC (char, len);
1189       snprintf (iterators[i], len, "git_%d", i);
1190     }
1191
1192   cloog_names_set_nb_iterators (cloog_program_names (prog),
1193                                 nb_iterators);
1194   cloog_names_set_iterators (cloog_program_names (prog),
1195                              iterators);
1196
1197   for (i = 0; i < nb_scattering; i++)
1198     {
1199       int len = 5 + 16;
1200       scattering[i] = XNEWVEC (char, len);
1201       snprintf (scattering[i], len, "scat_%d", i);
1202     }
1203
1204   cloog_names_set_nb_scattering (cloog_program_names (prog),
1205                                  nb_scattering);
1206   cloog_names_set_scattering (cloog_program_names (prog),
1207                               scattering);
1208 }
1209
1210 /* Initialize a CLooG input file.  */
1211
1212 static FILE *
1213 init_cloog_input_file (int scop_number)
1214 {
1215   FILE *graphite_out_file;
1216   int len = strlen (dump_base_name);
1217   char *dumpname = XNEWVEC (char, len + 25);
1218   char *s_scop_number = XNEWVEC (char, 15);
1219
1220   memcpy (dumpname, dump_base_name, len + 1);
1221   strip_off_ending (dumpname, len);
1222   sprintf (s_scop_number, ".%d", scop_number);
1223   strcat (dumpname, s_scop_number);
1224   strcat (dumpname, ".cloog");
1225   graphite_out_file = fopen (dumpname, "w+b");
1226
1227   if (graphite_out_file == 0)
1228     fatal_error ("can%'t open %s for writing: %m", dumpname);
1229
1230   free (dumpname);
1231
1232   return graphite_out_file;
1233 }
1234
1235 /* Build cloog program for SCoP.  */
1236
1237 static void
1238 build_cloog_prog (scop_p scop, CloogProgram *prog,
1239                   CloogOptions *options, CloogState *state ATTRIBUTE_UNUSED)
1240 {
1241   int i;
1242   int max_nb_loops = scop_max_loop_depth (scop);
1243   poly_bb_p pbb;
1244   CloogLoop *loop_list = NULL;
1245   CloogBlockList *block_list = NULL;
1246   CloogScatteringList *scattering = NULL;
1247   int nbs = 2 * max_nb_loops + 1;
1248   int *scaldims;
1249
1250   cloog_program_set_context
1251     (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop),
1252       scop_nb_params (scop), state));
1253   nbs = unify_scattering_dimensions (scop);
1254   scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1255   cloog_program_set_nb_scattdims (prog, nbs);
1256   initialize_cloog_names (scop, prog);
1257
1258   FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1259     {
1260       CloogStatement *stmt;
1261       CloogBlock *block;
1262       CloogDomain *dom;
1263
1264       /* Dead code elimination: when the domain of a PBB is empty,
1265          don't generate code for the PBB.  */
1266       if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1267         continue;
1268
1269       /* Build the new statement and its block.  */
1270       stmt = cloog_statement_alloc (state, pbb_index (pbb));
1271       dom = new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb),
1272                                                          scop_nb_params (scop),
1273                                                          state);
1274       block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1275       cloog_statement_set_usr (stmt, pbb);
1276
1277       /* Build loop list.  */
1278       {
1279         CloogLoop *new_loop_list = cloog_loop_malloc (state);
1280         cloog_loop_set_next (new_loop_list, loop_list);
1281         cloog_loop_set_domain (new_loop_list, dom);
1282         cloog_loop_set_block (new_loop_list, block);
1283         loop_list = new_loop_list;
1284       }
1285
1286       /* Build block list.  */
1287       {
1288         CloogBlockList *new_block_list = cloog_block_list_malloc ();
1289
1290         cloog_block_list_set_next (new_block_list, block_list);
1291         cloog_block_list_set_block (new_block_list, block);
1292         block_list = new_block_list;
1293       }
1294
1295       /* Build scattering list.  */
1296       {
1297         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
1298         CloogScatteringList *new_scattering
1299           = (CloogScatteringList *) xmalloc (sizeof (CloogScatteringList));
1300         ppl_Polyhedron_t scat;
1301         CloogScattering *dom;
1302
1303         scat = PBB_TRANSFORMED_SCATTERING (pbb);
1304         dom = new_Cloog_Scattering_from_ppl_Polyhedron
1305           (scat, scop_nb_params (scop), pbb_nb_scattering_transform (pbb),
1306            state);
1307
1308         cloog_set_next_scattering (new_scattering, scattering);
1309         cloog_set_scattering (new_scattering, dom);
1310         scattering = new_scattering;
1311       }
1312     }
1313
1314   cloog_program_set_loop (prog, loop_list);
1315   cloog_program_set_blocklist (prog, block_list);
1316
1317   for (i = 0; i < nbs; i++)
1318     scaldims[i] = 0 ;
1319
1320   cloog_program_set_scaldims (prog, scaldims);
1321
1322   /* Extract scalar dimensions to simplify the code generation problem.  */
1323   cloog_program_extract_scalars (prog, scattering, options);
1324
1325   /* Dump a .cloog input file, if requested.  This feature is only
1326      enabled in the Graphite branch.  */
1327   if (0)
1328     {
1329       static size_t file_scop_number = 0;
1330       FILE *cloog_file = init_cloog_input_file (file_scop_number);
1331
1332       cloog_program_dump_cloog (cloog_file, prog, scattering);
1333       ++file_scop_number;
1334     }
1335
1336   /* Apply scattering.  */
1337   cloog_program_scatter (prog, scattering, options);
1338   free_scattering (scattering);
1339
1340   /* Iterators corresponding to scalar dimensions have to be extracted.  */
1341   cloog_names_scalarize (cloog_program_names (prog), nbs,
1342                          cloog_program_scaldims (prog));
1343
1344   /* Free blocklist.  */
1345   {
1346     CloogBlockList *next = cloog_program_blocklist (prog);
1347
1348     while (next)
1349       {
1350         CloogBlockList *toDelete = next;
1351         next = cloog_block_list_next (next);
1352         cloog_block_list_set_next (toDelete, NULL);
1353         cloog_block_list_set_block (toDelete, NULL);
1354         cloog_block_list_free (toDelete);
1355       }
1356     cloog_program_set_blocklist (prog, NULL);
1357   }
1358 }
1359
1360 /* Return the options that will be used in GLOOG.  */
1361
1362 static CloogOptions *
1363 set_cloog_options (CloogState *state ATTRIBUTE_UNUSED)
1364 {
1365   CloogOptions *options = cloog_options_malloc (state);
1366
1367   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
1368      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1369      we pass an incomplete program to cloog.  */
1370   options->language = LANGUAGE_C;
1371
1372   /* Enable complex equality spreading: removes dummy statements
1373      (assignments) in the generated code which repeats the
1374      substitution equations for statements.  This is useless for
1375      GLooG.  */
1376   options->esp = 1;
1377
1378 #ifdef CLOOG_ORG
1379   /* Silence CLooG to avoid failing tests due to debug output to stderr.  */
1380   options->quiet = 1;
1381 #else
1382   /* Enable C pretty-printing mode: normalizes the substitution
1383      equations for statements.  */
1384   options->cpp = 1;
1385 #endif
1386
1387   /* Allow cloog to build strides with a stride width different to one.
1388      This example has stride = 4:
1389
1390      for (i = 0; i < 20; i += 4)
1391        A  */
1392   options->strides = 1;
1393
1394   /* Disable optimizations and make cloog generate source code closer to the
1395      input.  This is useful for debugging,  but later we want the optimized
1396      code.
1397
1398      XXX: We can not disable optimizations, as loop blocking is not working
1399      without them.  */
1400   if (0)
1401     {
1402       options->f = -1;
1403       options->l = INT_MAX;
1404     }
1405
1406   return options;
1407 }
1408
1409 /* Prints STMT to STDERR.  */
1410
1411 void
1412 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1413 {
1414   CloogState *state = cloog_state_malloc ();
1415   CloogOptions *options = set_cloog_options (state);
1416
1417   clast_pprint (file, stmt, 0, options);
1418   cloog_options_free (options);
1419   cloog_state_free (state);
1420 }
1421
1422 /* Prints STMT to STDERR.  */
1423
1424 DEBUG_FUNCTION void
1425 debug_clast_stmt (struct clast_stmt *stmt)
1426 {
1427   print_clast_stmt (stderr, stmt);
1428 }
1429
1430 /* Translate SCOP to a CLooG program and clast.  These two
1431    representations should be freed together: a clast cannot be used
1432    without a program.  */
1433
1434 cloog_prog_clast
1435 scop_to_clast (scop_p scop, CloogState *state)
1436 {
1437   CloogOptions *options = set_cloog_options (state);
1438   cloog_prog_clast pc;
1439
1440   /* Connect new cloog prog generation to graphite.  */
1441   pc.prog = cloog_program_malloc ();
1442   build_cloog_prog (scop, pc.prog, options, state);
1443   pc.prog = cloog_program_generate (pc.prog, options);
1444   pc.stmt = cloog_clast_create (pc.prog, options);
1445
1446   cloog_options_free (options);
1447   return pc;
1448 }
1449
1450 /* Prints to FILE the code generated by CLooG for SCOP.  */
1451
1452 void
1453 print_generated_program (FILE *file, scop_p scop)
1454 {
1455   CloogState *state = cloog_state_malloc ();
1456   CloogOptions *options = set_cloog_options (state);
1457
1458   cloog_prog_clast pc = scop_to_clast (scop, state);
1459
1460   fprintf (file, "       (prog: \n");
1461   cloog_program_print (file, pc.prog);
1462   fprintf (file, "       )\n");
1463
1464   fprintf (file, "       (clast: \n");
1465   clast_pprint (file, pc.stmt, 0, options);
1466   fprintf (file, "       )\n");
1467
1468   cloog_options_free (options);
1469   cloog_clast_free (pc.stmt);
1470   cloog_program_free (pc.prog);
1471 }
1472
1473 /* Prints to STDERR the code generated by CLooG for SCOP.  */
1474
1475 DEBUG_FUNCTION void
1476 debug_generated_program (scop_p scop)
1477 {
1478   print_generated_program (stderr, scop);
1479 }
1480
1481 /* Add CLooG names to parameter index.  The index is used to translate
1482    back from CLooG names to GCC trees.  */
1483
1484 static void
1485 create_params_index (htab_t index_table, CloogProgram *prog) {
1486   CloogNames* names = cloog_program_names (prog);
1487   int nb_parameters = cloog_names_nb_parameters (names);
1488   char **parameters = cloog_names_parameters (names);
1489   int i;
1490
1491   for (i = 0; i < nb_parameters; i++)
1492     save_clast_name_index (index_table, parameters[i], i);
1493 }
1494
1495 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1496    the given SCOP.  Return true if code generation succeeded.
1497    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1498 */
1499
1500 bool
1501 gloog (scop_p scop, htab_t bb_pbb_mapping)
1502 {
1503   VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1504   loop_p context_loop;
1505   sese region = SCOP_REGION (scop);
1506   ifsese if_region = NULL;
1507   htab_t newivs_index, params_index;
1508   cloog_prog_clast pc;
1509   CloogState *state;
1510
1511   state = cloog_state_malloc ();
1512   timevar_push (TV_GRAPHITE_CODE_GEN);
1513   gloog_error = false;
1514
1515   pc = scop_to_clast (scop, state);
1516
1517   if (dump_file && (dump_flags & TDF_DETAILS))
1518     {
1519       fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1520       print_clast_stmt (dump_file, pc.stmt);
1521       fprintf (dump_file, "\n");
1522     }
1523
1524   recompute_all_dominators ();
1525   graphite_verify ();
1526
1527   if_region = move_sese_in_condition (region);
1528   sese_insert_phis_for_liveouts (region,
1529                                  if_region->region->exit->src,
1530                                  if_region->false_region->exit,
1531                                  if_region->true_region->exit);
1532   recompute_all_dominators ();
1533   graphite_verify ();
1534
1535   context_loop = SESE_ENTRY (region)->src->loop_father;
1536   newivs_index = htab_create (10, clast_name_index_elt_info,
1537                               eq_clast_name_indexes, free);
1538   params_index = htab_create (10, clast_name_index_elt_info,
1539                               eq_clast_name_indexes, free);
1540
1541   create_params_index (params_index, pc.prog);
1542
1543   translate_clast (region, context_loop, pc.stmt,
1544                    if_region->true_region->entry,
1545                    &newivs, newivs_index,
1546                    bb_pbb_mapping, 1, params_index);
1547   graphite_verify ();
1548   scev_reset ();
1549   recompute_all_dominators ();
1550   graphite_verify ();
1551
1552   if (gloog_error)
1553     set_ifsese_condition (if_region, integer_zero_node);
1554
1555   free (if_region->true_region);
1556   free (if_region->region);
1557   free (if_region);
1558
1559   htab_delete (newivs_index);
1560   htab_delete (params_index);
1561   VEC_free (tree, heap, newivs);
1562   cloog_clast_free (pc.stmt);
1563   cloog_program_free (pc.prog);
1564   timevar_pop (TV_GRAPHITE_CODE_GEN);
1565
1566   if (dump_file && (dump_flags & TDF_DETAILS))
1567     {
1568       loop_p loop;
1569       loop_iterator li;
1570       int num_no_dependency = 0;
1571
1572       FOR_EACH_LOOP (li, loop, 0)
1573         if (loop->can_be_parallel)
1574           num_no_dependency++;
1575
1576       fprintf (dump_file, "\n%d loops carried no dependency.\n",
1577                num_no_dependency);
1578     }
1579
1580   cloog_state_free (state);
1581
1582   return !gloog_error;
1583 }
1584 #endif