OSDN Git Service

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