OSDN Git Service

2010-09-01 Janus Weil <janus@gcc.gnu.org>
[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 (clast_name_p name, htab_t index_table)
101 {
102   struct clast_name_index tmp;
103   PTR *slot;
104
105 #ifdef CLOOG_ORG
106   gcc_assert (name->type == clast_expr_name);
107   tmp.name = ((const struct clast_name*) name)->name;
108 #else
109   tmp.name = name;
110 #endif
111
112   slot = htab_find_slot (index_table, &tmp, NO_INSERT);
113
114   if (slot && *slot)
115     return ((struct clast_name_index *) *slot)->index;
116
117   return -1;
118 }
119
120 /* Records in INDEX_TABLE the INDEX for NAME.  */
121
122 static inline void
123 save_clast_name_index (htab_t index_table, const char *name, int index)
124 {
125   struct clast_name_index tmp;
126   PTR *slot;
127
128   tmp.name = name;
129   slot = htab_find_slot (index_table, &tmp, INSERT);
130
131   if (slot)
132     {
133       if (*slot)
134         free (*slot);
135
136       *slot = new_clast_name_index (name, index);
137     }
138 }
139
140 /* Computes a hash function for database element ELT.  */
141
142 static inline hashval_t
143 clast_name_index_elt_info (const void *elt)
144 {
145   return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
146 }
147
148 /* Compares database elements E1 and E2.  */
149
150 static inline int
151 eq_clast_name_indexes (const void *e1, const void *e2)
152 {
153   const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
154   const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
155
156   return (elt1->name == elt2->name);
157 }
158
159 /* For a given scattering dimension, return the new induction variable
160    associated to it.  */
161
162 static inline tree
163 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
164 {
165   return VEC_index (tree, newivs, depth);
166 }
167
168 \f
169
170 /* Returns the tree variable from the name NAME that was given in
171    Cloog representation.  */
172
173 static tree
174 clast_name_to_gcc (clast_name_p name, sese region, VEC (tree, heap) *newivs,
175                    htab_t newivs_index, htab_t params_index)
176 {
177   int index;
178   VEC (tree, heap) *params = SESE_PARAMS (region);
179
180   if (params && params_index)
181     {
182       index = clast_name_to_index (name, params_index);
183
184       if (index >= 0)
185         return VEC_index (tree, params, index);
186     }
187
188   gcc_assert (newivs && newivs_index);
189   index = clast_name_to_index (name, newivs_index);
190   gcc_assert (index >= 0);
191
192   return newivs_to_depth_to_newiv (newivs, index);
193 }
194
195 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2.  */
196
197 static tree
198 max_signed_precision_type (tree type1, tree type2)
199 {
200   int p1 = TYPE_PRECISION (type1);
201   int p2 = TYPE_PRECISION (type2);
202   int precision;
203   tree type;
204
205   if (p1 > p2)
206     precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
207   else
208     precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
209
210   type = lang_hooks.types.type_for_size (precision, false);
211
212   if (!type)
213     {
214       gloog_error = true;
215       return integer_type_node;
216     }
217   return type;
218 }
219
220 /* Returns the maximal precision type for expressions TYPE1 and TYPE2.  */
221
222 static tree
223 max_precision_type (tree type1, tree type2)
224 {
225   if (POINTER_TYPE_P (type1))
226     return type1;
227
228   if (POINTER_TYPE_P (type2))
229     return type2;
230
231   if (!TYPE_UNSIGNED (type1)
232       || !TYPE_UNSIGNED (type2))
233     return max_signed_precision_type (type1, type2);
234
235   return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
236 }
237
238 static tree
239 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
240                          htab_t, htab_t);
241
242 /* Converts a Cloog reduction expression R with reduction operation OP
243    to a GCC expression tree of type TYPE.  */
244
245 static tree
246 clast_to_gcc_expression_red (tree type, enum tree_code op,
247                              struct clast_reduction *r,
248                              sese region, VEC (tree, heap) *newivs,
249                              htab_t newivs_index, htab_t params_index)
250 {
251   int i;
252   tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
253                                       newivs_index, params_index);
254   tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
255
256   for (i = 1; i < r->n; i++)
257     {
258       tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
259                                         newivs, newivs_index, params_index);
260       res = fold_build2 (op, type, res, t);
261     }
262
263   return res;
264 }
265
266 /* Converts a Cloog AST expression E back to a GCC expression tree of
267    type TYPE.  */
268
269 static tree
270 clast_to_gcc_expression (tree type, struct clast_expr *e,
271                          sese region, VEC (tree, heap) *newivs,
272                          htab_t newivs_index, htab_t params_index)
273 {
274   switch (e->type)
275     {
276     case clast_expr_term:
277       {
278         struct clast_term *t = (struct clast_term *) e;
279
280         if (t->var)
281           {
282             if (mpz_cmp_si (t->val, 1) == 0)
283               {
284                 tree name = clast_name_to_gcc (t->var, region, newivs,
285                                                newivs_index, params_index);
286
287                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
288                   name = fold_convert (sizetype, name);
289
290                 name = fold_convert (type, name);
291                 return name;
292               }
293
294             else if (mpz_cmp_si (t->val, -1) == 0)
295               {
296                 tree name = clast_name_to_gcc (t->var, region, newivs,
297                                                newivs_index, params_index);
298
299                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
300                   name = fold_convert (sizetype, name);
301
302                 name = fold_convert (type, name);
303
304                 return fold_build1 (NEGATE_EXPR, type, name);
305               }
306             else
307               {
308                 tree name = clast_name_to_gcc (t->var, region, newivs,
309                                                newivs_index, params_index);
310                 tree cst = gmp_cst_to_tree (type, t->val);
311
312                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
313                   name = fold_convert (sizetype, name);
314
315                 name = fold_convert (type, name);
316
317                 if (!POINTER_TYPE_P (type))
318                   return fold_build2 (MULT_EXPR, type, cst, name);
319
320                 gloog_error = true;
321                 return cst;
322               }
323           }
324         else
325           return gmp_cst_to_tree (type, t->val);
326       }
327
328     case clast_expr_red:
329       {
330         struct clast_reduction *r = (struct clast_reduction *) e;
331
332         switch (r->type)
333           {
334           case clast_red_sum:
335             return clast_to_gcc_expression_red
336               (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
337                r, region, newivs, newivs_index, params_index);
338
339           case clast_red_min:
340             return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
341                                                 newivs, newivs_index,
342                                                 params_index);
343
344           case clast_red_max:
345             return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
346                                                 newivs, newivs_index,
347                                                 params_index);
348
349           default:
350             gcc_unreachable ();
351           }
352         break;
353       }
354
355     case clast_expr_bin:
356       {
357         struct clast_binary *b = (struct clast_binary *) e;
358         struct clast_expr *lhs = (struct clast_expr *) b->LHS;
359         tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
360                                            newivs_index, params_index);
361         tree tr = gmp_cst_to_tree (type, b->RHS);
362
363         switch (b->type)
364           {
365           case clast_bin_fdiv:
366             return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
367
368           case clast_bin_cdiv:
369             return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
370
371           case clast_bin_div:
372             return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
373
374           case clast_bin_mod:
375             return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
376
377           default:
378             gcc_unreachable ();
379           }
380       }
381
382     default:
383       gcc_unreachable ();
384     }
385
386   return NULL_TREE;
387 }
388
389 /* Return the precision needed to represent the value VAL.  */
390
391 static int
392 precision_for_value (mpz_t val)
393 {
394   mpz_t x, y, two;
395   int precision;
396
397   mpz_init (x);
398   mpz_init (y);
399   mpz_init (two);
400   mpz_set_si (x, 2);
401   mpz_set (y, val);
402   mpz_set_si (two, 2);
403   precision = 1;
404
405   if (mpz_sgn (y) < 0)
406     mpz_neg (y, y);
407
408   while (mpz_cmp (y, x) > 0)
409     {
410       mpz_mul (x, x, two);
411       precision++;
412     }
413
414   mpz_clear (x);
415   mpz_clear (y);
416   mpz_clear (two);
417
418   return precision;
419 }
420
421 /* Return the precision needed to represent the values between LOW and
422    UP.  */
423
424 static int
425 precision_for_interval (mpz_t low, mpz_t up)
426 {
427   mpz_t diff;
428   int precision;
429
430   gcc_assert (mpz_cmp (low, up) <= 0);
431
432   mpz_init (diff);
433   mpz_sub (diff, up, low);
434   precision = precision_for_value (diff);
435   mpz_clear (diff);
436
437   return precision;
438 }
439
440 /* Return a type that could represent the integer value VAL.  */
441
442 static tree
443 gcc_type_for_interval (mpz_t low, mpz_t up)
444 {
445   bool unsigned_p = true;
446   int precision, prec_up, prec_int;
447   tree type;
448   enum machine_mode mode;
449
450   gcc_assert (mpz_cmp (low, up) <= 0);
451
452   if (mpz_sgn (low) < 0)
453     unsigned_p = false;
454
455   prec_up = precision_for_value (up);
456   prec_int = precision_for_interval (low, up);
457   precision = MAX (prec_up, prec_int);
458
459   if (precision > BITS_PER_WORD)
460     {
461       gloog_error = true;
462       return integer_type_node;
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 }
692
693 /* Compute the type for the induction variable at LEVEL for the
694    statement PBB, based on the transformed schedule of PBB.  */
695
696 static tree
697 compute_type_for_level (poly_bb_p pbb, int level)
698 {
699   mpz_t low, up;
700   tree type;
701
702   mpz_init (low);
703   mpz_init (up);
704
705   compute_bounds_for_level (pbb, level, low, up);
706   type = gcc_type_for_interval (low, up);
707
708   mpz_clear (low);
709   mpz_clear (up);
710   return type;
711 }
712
713 /* Walks a CLAST and returns the first statement in the body of a
714    loop.  */
715
716 static struct clast_user_stmt *
717 clast_get_body_of_loop (struct clast_stmt *stmt)
718 {
719   if (!stmt
720       || CLAST_STMT_IS_A (stmt, stmt_user))
721     return (struct clast_user_stmt *) stmt;
722
723   if (CLAST_STMT_IS_A (stmt, stmt_for))
724     return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
725
726   if (CLAST_STMT_IS_A (stmt, stmt_guard))
727     return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
728
729   if (CLAST_STMT_IS_A (stmt, stmt_block))
730     return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
731
732   gcc_unreachable ();
733 }
734
735 /* Returns the type for the induction variable for the loop translated
736    from STMT_FOR.  */
737
738 static tree
739 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
740                                tree lb_type, tree ub_type)
741 {
742   struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
743   struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
744   CloogStatement *cs = body->statement;
745   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
746
747   return max_signed_precision_type (lb_type, max_precision_type
748                                     (ub_type, compute_type_for_level
749                                      (pbb, level - 1)));
750 }
751
752 /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an
753    induction variable for the new LOOP.  New LOOP is attached to CFG
754    starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
755    becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
756    CLooG's scattering name to the induction variable created for the
757    loop of STMT.  The new induction variable is inserted in the NEWIVS
758    vector.  */
759
760 static struct loop *
761 graphite_create_new_loop (sese region, edge entry_edge,
762                           struct clast_for *stmt,
763                           loop_p outer, VEC (tree, heap) **newivs,
764                           htab_t newivs_index, htab_t params_index, int level)
765 {
766   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
767                                           newivs_index, params_index);
768   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
769                                           newivs_index, params_index);
770   tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
771   tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
772                                      newivs_index, params_index);
773   tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
774                                      newivs_index, params_index);
775   tree stride = gmp_cst_to_tree (type, stmt->stride);
776   tree ivvar = create_tmp_var (type, "graphite_IV");
777   tree iv, iv_after_increment;
778   loop_p loop = create_empty_loop_on_edge
779     (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
780      outer ? outer : entry_edge->src->loop_father);
781
782   add_referenced_var (ivvar);
783
784   save_clast_name_index (newivs_index, stmt->iterator,
785                          VEC_length (tree, *newivs));
786   VEC_safe_push (tree, heap, *newivs, iv);
787   return loop;
788 }
789
790 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
791    induction variables of the loops around GBB in SESE.  */
792
793 static void
794 build_iv_mapping (VEC (tree, heap) *iv_map, sese region,
795                   VEC (tree, heap) *newivs, htab_t newivs_index,
796                   struct clast_user_stmt *user_stmt,
797                   htab_t params_index)
798 {
799   struct clast_stmt *t;
800   int depth = 0;
801   CloogStatement *cs = user_stmt->statement;
802   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
803   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
804
805   for (t = user_stmt->substitutions; t; t = t->next, depth++)
806     {
807       struct clast_expr *expr = (struct clast_expr *)
808        ((struct clast_assignment *)t)->RHS;
809       tree type = gcc_type_for_clast_expr (expr, region, newivs,
810                                            newivs_index, params_index);
811       tree new_name = clast_to_gcc_expression (type, expr, region, newivs,
812                                                newivs_index, params_index);
813       loop_p old_loop = gbb_loop_at_index (gbb, region, depth);
814
815       VEC_replace (tree, iv_map, old_loop->num, new_name);
816     }
817 }
818
819 /* Construct bb_pbb_def with BB and PBB.  */
820
821 static bb_pbb_def *
822 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
823 {
824   bb_pbb_def *bb_pbb_p;
825
826   bb_pbb_p = XNEW (bb_pbb_def);
827   bb_pbb_p->bb = bb;
828   bb_pbb_p->pbb = pbb;
829
830   return bb_pbb_p;
831 }
832
833 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING.  */
834
835 static void
836 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
837 {
838   bb_pbb_def tmp;
839   PTR *x;
840
841   tmp.bb = bb;
842   x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
843
844   if (x && !*x)
845     *x = new_bb_pbb_def (bb, pbb);
846 }
847
848 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING.  */
849
850 static poly_bb_p
851 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
852 {
853   bb_pbb_def tmp;
854   PTR *slot;
855
856   tmp.bb = bb;
857   slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
858
859   if (slot && *slot)
860     return ((bb_pbb_def *) *slot)->pbb;
861
862   return NULL;
863 }
864
865 /* Check data dependency in LOOP at scattering level LEVEL.
866    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
867    mapping.  */
868
869 static bool
870 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
871 {
872   unsigned i,j;
873   basic_block *bbs = get_loop_body_in_dom_order (loop);
874
875   for (i = 0; i < loop->num_nodes; i++)
876     {
877       poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
878
879       if (pbb1 == NULL)
880        continue;
881
882       for (j = 0; j < loop->num_nodes; j++)
883        {
884          poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
885
886          if (pbb2 == NULL)
887            continue;
888
889          if (dependency_between_pbbs_p (pbb1, pbb2, level))
890            {
891              free (bbs);
892              return true;
893            }
894        }
895     }
896
897   free (bbs);
898
899   return false;
900 }
901
902 /* Translates a clast user statement STMT to gimple.
903
904    - REGION is the sese region we used to generate the scop.
905    - NEXT_E is the edge where new generated code should be attached.
906    - CONTEXT_LOOP is the loop in which the generated code will be placed
907    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
908    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
909      the sese region.  */
910 static edge
911 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
912                       VEC (tree, heap) **newivs,
913                       htab_t newivs_index, htab_t bb_pbb_mapping,
914                       htab_t params_index)
915 {
916   int i, nb_loops;
917   basic_block new_bb;
918   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
919   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
920   VEC (tree, heap) *iv_map;
921
922   if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
923     return next_e;
924
925   nb_loops = number_of_loops ();
926   iv_map = VEC_alloc (tree, heap, nb_loops);
927   for (i = 0; i < nb_loops; i++)
928     VEC_quick_push (tree, iv_map, NULL_TREE);
929
930   build_iv_mapping (iv_map, region, *newivs, newivs_index, stmt, params_index);
931   next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
932                                            next_e, iv_map);
933   VEC_free (tree, heap, iv_map);
934
935   new_bb = next_e->src;
936   mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
937   update_ssa (TODO_update_ssa);
938
939   return next_e;
940 }
941
942 /* Creates a new if region protecting the loop to be executed, if the execution
943    count is zero (lb > ub).  */
944
945 static edge
946 graphite_create_new_loop_guard (sese region, edge entry_edge,
947                                 struct clast_for *stmt,
948                                 VEC (tree, heap) *newivs,
949                                 htab_t newivs_index, htab_t params_index)
950 {
951   tree cond_expr;
952   edge exit_edge;
953   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
954                                           newivs_index, params_index);
955   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
956                                           newivs_index, params_index);
957   tree type = max_precision_type (lb_type, ub_type);
958   tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
959                                      newivs_index, params_index);
960   tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
961                                      newivs_index, params_index);
962   tree one = POINTER_TYPE_P (type) ? size_one_node
963     : fold_convert (type, integer_one_node);
964   /* Adding +1 and using LT_EXPR helps with loop latches that have a
965      loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
966      2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
967      However lb < ub + 1 is false, as expected.  */
968   tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
969                              : PLUS_EXPR, type, ub, one);
970
971   /* When ub + 1 wraps around, use lb <= ub.  */
972   if (integer_zerop (ub_one))
973     cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
974   else
975     cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
976
977   exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
978
979   return exit_edge;
980 }
981
982 static edge
983 translate_clast (sese, loop_p, struct clast_stmt *, edge,
984                  VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
985
986 /* Create the loop for a clast for statement.
987
988    - REGION is the sese region we used to generate the scop.
989    - NEXT_E is the edge where new generated code should be attached.
990    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
991    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
992      the sese region.  */
993 static edge
994 translate_clast_for_loop (sese region, loop_p context_loop,
995                           struct clast_for *stmt, edge next_e,
996                           VEC (tree, heap) **newivs,
997                           htab_t newivs_index, htab_t bb_pbb_mapping,
998                           int level, htab_t params_index)
999 {
1000   struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1001                                                 context_loop, newivs,
1002                                                 newivs_index, params_index,
1003                                                 level);
1004   edge last_e = single_exit (loop);
1005   edge to_body = single_succ_edge (loop->header);
1006   basic_block after = to_body->dest;
1007
1008   /* Create a basic block for loop close phi nodes.  */
1009   last_e = single_succ_edge (split_edge (last_e));
1010
1011   /* Translate the body of the loop.  */
1012   next_e = translate_clast (region, loop, stmt->body, to_body,
1013                             newivs, newivs_index, bb_pbb_mapping, level + 1,
1014                             params_index);
1015   redirect_edge_succ_nodup (next_e, after);
1016   set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1017
1018   if (flag_loop_parallelize_all
1019       && !dependency_in_loop_p (loop, bb_pbb_mapping,
1020                                 get_scattering_level (level)))
1021     loop->can_be_parallel = true;
1022
1023   return last_e;
1024 }
1025
1026 /* Translates a clast for statement STMT to gimple.  First a guard is created
1027    protecting the loop, if it is executed zero times.  In this guard we create
1028    the real loop structure.
1029
1030    - REGION is the sese region we used to generate the scop.
1031    - NEXT_E is the edge where new generated code should be attached.
1032    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1033    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1034      the sese region.  */
1035 static edge
1036 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1037                      edge next_e, VEC (tree, heap) **newivs,
1038                      htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1039                      htab_t params_index)
1040 {
1041   edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1042                                                 newivs_index, params_index);
1043   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1044
1045   translate_clast_for_loop (region, context_loop, stmt, true_e, newivs,
1046                             newivs_index, bb_pbb_mapping, level,
1047                             params_index);
1048   return last_e;
1049 }
1050
1051 /* Translates a clast guard statement STMT to gimple.
1052
1053    - REGION is the sese region we used to generate the scop.
1054    - NEXT_E is the edge where new generated code should be attached.
1055    - CONTEXT_LOOP is the loop in which the generated code will be placed
1056    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1057    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1058      the sese region.  */
1059 static edge
1060 translate_clast_guard (sese region, loop_p context_loop,
1061                        struct clast_guard *stmt, edge next_e,
1062                        VEC (tree, heap) **newivs,
1063                        htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1064                        htab_t params_index)
1065 {
1066   edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1067                                            newivs_index, params_index);
1068   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1069
1070   translate_clast (region, context_loop, stmt->then, true_e,
1071                    newivs, newivs_index, bb_pbb_mapping,
1072                    level, params_index);
1073   return last_e;
1074 }
1075
1076 /* Translates a CLAST statement STMT to GCC representation in the
1077    context of a SESE.
1078
1079    - NEXT_E is the edge where new generated code should be attached.
1080    - CONTEXT_LOOP is the loop in which the generated code will be placed
1081    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1082 static edge
1083 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1084                  edge next_e, VEC (tree, heap) **newivs,
1085                  htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1086                  htab_t params_index)
1087 {
1088   if (!stmt)
1089     return next_e;
1090
1091   if (CLAST_STMT_IS_A (stmt, stmt_root))
1092     ; /* Do nothing.  */
1093
1094   else if (CLAST_STMT_IS_A (stmt, stmt_user))
1095     next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1096                                    next_e, newivs, newivs_index,
1097                                    bb_pbb_mapping, params_index);
1098
1099   else if (CLAST_STMT_IS_A (stmt, stmt_for))
1100     next_e = translate_clast_for (region, context_loop,
1101                                   (struct clast_for *) stmt, next_e,
1102                                   newivs, newivs_index,
1103                                   bb_pbb_mapping, level, params_index);
1104
1105   else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1106     next_e = translate_clast_guard (region, context_loop,
1107                                     (struct clast_guard *) stmt, next_e,
1108                                     newivs, newivs_index,
1109                                     bb_pbb_mapping, level, params_index);
1110
1111   else if (CLAST_STMT_IS_A (stmt, stmt_block))
1112     next_e = translate_clast (region, context_loop,
1113                               ((struct clast_block *) stmt)->body,
1114                               next_e, newivs, newivs_index,
1115                               bb_pbb_mapping, level, params_index);
1116   else
1117     gcc_unreachable();
1118
1119   recompute_all_dominators ();
1120   graphite_verify ();
1121
1122   return translate_clast (region, context_loop, stmt->next, next_e,
1123                           newivs, newivs_index,
1124                           bb_pbb_mapping, level, params_index);
1125 }
1126
1127 /* Free the SCATTERING domain list.  */
1128
1129 static void
1130 free_scattering (CloogScatteringList *scattering)
1131 {
1132   while (scattering)
1133     {
1134       CloogScattering *dom = cloog_scattering (scattering);
1135       CloogScatteringList *next = cloog_next_scattering (scattering);
1136
1137       cloog_scattering_free (dom);
1138       free (scattering);
1139       scattering = next;
1140     }
1141 }
1142
1143 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1144    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1145    from 0 to scop_nb_loops (scop).  */
1146
1147 static void
1148 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1149 {
1150   sese region = SCOP_REGION (scop);
1151   int i;
1152   int nb_iterators = scop_max_loop_depth (scop);
1153   int nb_scattering = cloog_program_nb_scattdims (prog);
1154   int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1155   char **iterators = XNEWVEC (char *, nb_iterators * 2);
1156   char **scattering = XNEWVEC (char *, nb_scattering);
1157   char **parameters= XNEWVEC (char *, nb_parameters);
1158
1159   cloog_program_set_names (prog, cloog_names_malloc ());
1160
1161   for (i = 0; i < nb_parameters; i++)
1162     {
1163       tree param = VEC_index (tree, SESE_PARAMS(region), i);
1164       const char *name = get_name (param);
1165       int len;
1166
1167       if (!name)
1168         name = "T";
1169
1170       len = strlen (name);
1171       len += 17;
1172       parameters[i] = XNEWVEC (char, len + 1);
1173       snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1174     }
1175
1176   cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1177   cloog_names_set_parameters (cloog_program_names (prog), parameters);
1178
1179   for (i = 0; i < nb_iterators; i++)
1180     {
1181       int len = 4 + 16;
1182       iterators[i] = XNEWVEC (char, len);
1183       snprintf (iterators[i], len, "git_%d", i);
1184     }
1185
1186   cloog_names_set_nb_iterators (cloog_program_names (prog),
1187                                 nb_iterators);
1188   cloog_names_set_iterators (cloog_program_names (prog),
1189                              iterators);
1190
1191   for (i = 0; i < nb_scattering; i++)
1192     {
1193       int len = 5 + 16;
1194       scattering[i] = XNEWVEC (char, len);
1195       snprintf (scattering[i], len, "scat_%d", i);
1196     }
1197
1198   cloog_names_set_nb_scattering (cloog_program_names (prog),
1199                                  nb_scattering);
1200   cloog_names_set_scattering (cloog_program_names (prog),
1201                               scattering);
1202 }
1203
1204 /* Build cloog program for SCoP.  */
1205
1206 static void
1207 build_cloog_prog (scop_p scop, CloogProgram *prog,
1208                   CloogOptions *options, CloogState *state ATTRIBUTE_UNUSED)
1209 {
1210   int i;
1211   int max_nb_loops = scop_max_loop_depth (scop);
1212   poly_bb_p pbb;
1213   CloogLoop *loop_list = NULL;
1214   CloogBlockList *block_list = NULL;
1215   CloogScatteringList *scattering = NULL;
1216   int nbs = 2 * max_nb_loops + 1;
1217   int *scaldims;
1218
1219   cloog_program_set_context
1220     (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop),
1221       scop_nb_params (scop), state));
1222   nbs = unify_scattering_dimensions (scop);
1223   scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1224   cloog_program_set_nb_scattdims (prog, nbs);
1225   initialize_cloog_names (scop, prog);
1226
1227   FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1228     {
1229       CloogStatement *stmt;
1230       CloogBlock *block;
1231       CloogDomain *dom;
1232
1233       /* Dead code elimination: when the domain of a PBB is empty,
1234          don't generate code for the PBB.  */
1235       if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1236         continue;
1237
1238       /* Build the new statement and its block.  */
1239       stmt = cloog_statement_alloc (state, pbb_index (pbb));
1240       dom = new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb),
1241                                                          scop_nb_params (scop),
1242                                                          state);
1243       block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1244       cloog_statement_set_usr (stmt, pbb);
1245
1246       /* Build loop list.  */
1247       {
1248         CloogLoop *new_loop_list = cloog_loop_malloc (state);
1249         cloog_loop_set_next (new_loop_list, loop_list);
1250         cloog_loop_set_domain (new_loop_list, dom);
1251         cloog_loop_set_block (new_loop_list, block);
1252         loop_list = new_loop_list;
1253       }
1254
1255       /* Build block list.  */
1256       {
1257         CloogBlockList *new_block_list = cloog_block_list_malloc ();
1258
1259         cloog_block_list_set_next (new_block_list, block_list);
1260         cloog_block_list_set_block (new_block_list, block);
1261         block_list = new_block_list;
1262       }
1263
1264       /* Build scattering list.  */
1265       {
1266         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
1267         CloogScatteringList *new_scattering
1268           = (CloogScatteringList *) xmalloc (sizeof (CloogScatteringList));
1269         ppl_Polyhedron_t scat;
1270         CloogScattering *dom;
1271
1272         scat = PBB_TRANSFORMED_SCATTERING (pbb);
1273         dom = new_Cloog_Scattering_from_ppl_Polyhedron
1274           (scat, scop_nb_params (scop), pbb_nb_scattering_transform (pbb),
1275            state);
1276
1277         cloog_set_next_scattering (new_scattering, scattering);
1278         cloog_set_scattering (new_scattering, dom);
1279         scattering = new_scattering;
1280       }
1281     }
1282
1283   cloog_program_set_loop (prog, loop_list);
1284   cloog_program_set_blocklist (prog, block_list);
1285
1286   for (i = 0; i < nbs; i++)
1287     scaldims[i] = 0 ;
1288
1289   cloog_program_set_scaldims (prog, scaldims);
1290
1291   /* Extract scalar dimensions to simplify the code generation problem.  */
1292   cloog_program_extract_scalars (prog, scattering, options);
1293
1294   /* Apply scattering.  */
1295   cloog_program_scatter (prog, scattering, options);
1296   free_scattering (scattering);
1297
1298   /* Iterators corresponding to scalar dimensions have to be extracted.  */
1299   cloog_names_scalarize (cloog_program_names (prog), nbs,
1300                          cloog_program_scaldims (prog));
1301
1302   /* Free blocklist.  */
1303   {
1304     CloogBlockList *next = cloog_program_blocklist (prog);
1305
1306     while (next)
1307       {
1308         CloogBlockList *toDelete = next;
1309         next = cloog_block_list_next (next);
1310         cloog_block_list_set_next (toDelete, NULL);
1311         cloog_block_list_set_block (toDelete, NULL);
1312         cloog_block_list_free (toDelete);
1313       }
1314     cloog_program_set_blocklist (prog, NULL);
1315   }
1316 }
1317
1318 /* Return the options that will be used in GLOOG.  */
1319
1320 static CloogOptions *
1321 set_cloog_options (CloogState *state ATTRIBUTE_UNUSED)
1322 {
1323   CloogOptions *options = cloog_options_malloc (state);
1324
1325   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
1326      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1327      we pass an incomplete program to cloog.  */
1328   options->language = LANGUAGE_C;
1329
1330   /* Enable complex equality spreading: removes dummy statements
1331      (assignments) in the generated code which repeats the
1332      substitution equations for statements.  This is useless for
1333      GLooG.  */
1334   options->esp = 1;
1335
1336 #ifdef CLOOG_ORG
1337   /* Silence CLooG to avoid failing tests due to debug output to stderr.  */
1338   options->quiet = 1;
1339 #else
1340   /* Enable C pretty-printing mode: normalizes the substitution
1341      equations for statements.  */
1342   options->cpp = 1;
1343 #endif
1344
1345   /* Allow cloog to build strides with a stride width different to one.
1346      This example has stride = 4:
1347
1348      for (i = 0; i < 20; i += 4)
1349        A  */
1350   options->strides = 1;
1351
1352   /* Disable optimizations and make cloog generate source code closer to the
1353      input.  This is useful for debugging,  but later we want the optimized
1354      code.
1355
1356      XXX: We can not disable optimizations, as loop blocking is not working
1357      without them.  */
1358   if (0)
1359     {
1360       options->f = -1;
1361       options->l = INT_MAX;
1362     }
1363
1364   return options;
1365 }
1366
1367 /* Prints STMT to STDERR.  */
1368
1369 void
1370 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1371 {
1372   CloogState *state = cloog_state_malloc ();
1373   CloogOptions *options = set_cloog_options (state);
1374
1375   clast_pprint (file, stmt, 0, options);
1376   cloog_options_free (options);
1377   cloog_state_free (state);
1378 }
1379
1380 /* Prints STMT to STDERR.  */
1381
1382 DEBUG_FUNCTION void
1383 debug_clast_stmt (struct clast_stmt *stmt)
1384 {
1385   print_clast_stmt (stderr, stmt);
1386 }
1387
1388 /* Translate SCOP to a CLooG program and clast.  These two
1389    representations should be freed together: a clast cannot be used
1390    without a program.  */
1391
1392 cloog_prog_clast
1393 scop_to_clast (scop_p scop, CloogState *state)
1394 {
1395   CloogOptions *options = set_cloog_options (state);
1396   cloog_prog_clast pc;
1397
1398   /* Connect new cloog prog generation to graphite.  */
1399   pc.prog = cloog_program_malloc ();
1400   build_cloog_prog (scop, pc.prog, options, state);
1401   pc.prog = cloog_program_generate (pc.prog, options);
1402   pc.stmt = cloog_clast_create (pc.prog, options);
1403
1404   cloog_options_free (options);
1405   return pc;
1406 }
1407
1408 /* Prints to FILE the code generated by CLooG for SCOP.  */
1409
1410 void
1411 print_generated_program (FILE *file, scop_p scop)
1412 {
1413   CloogState *state = cloog_state_malloc ();
1414   CloogOptions *options = set_cloog_options (state);
1415
1416   cloog_prog_clast pc = scop_to_clast (scop, state);
1417
1418   fprintf (file, "       (prog: \n");
1419   cloog_program_print (file, pc.prog);
1420   fprintf (file, "       )\n");
1421
1422   fprintf (file, "       (clast: \n");
1423   clast_pprint (file, pc.stmt, 0, options);
1424   fprintf (file, "       )\n");
1425
1426   cloog_options_free (options);
1427   cloog_clast_free (pc.stmt);
1428   cloog_program_free (pc.prog);
1429 }
1430
1431 /* Prints to STDERR the code generated by CLooG for SCOP.  */
1432
1433 DEBUG_FUNCTION void
1434 debug_generated_program (scop_p scop)
1435 {
1436   print_generated_program (stderr, scop);
1437 }
1438
1439 /* Add CLooG names to parameter index.  The index is used to translate
1440    back from CLooG names to GCC trees.  */
1441
1442 static void
1443 create_params_index (htab_t index_table, CloogProgram *prog) {
1444   CloogNames* names = cloog_program_names (prog);
1445   int nb_parameters = cloog_names_nb_parameters (names);
1446   char **parameters = cloog_names_parameters (names);
1447   int i;
1448
1449   for (i = 0; i < nb_parameters; i++)
1450     save_clast_name_index (index_table, parameters[i], i);
1451 }
1452
1453 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1454    the given SCOP.  Return true if code generation succeeded.
1455    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1456 */
1457
1458 bool
1459 gloog (scop_p scop, htab_t bb_pbb_mapping)
1460 {
1461   VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1462   loop_p context_loop;
1463   sese region = SCOP_REGION (scop);
1464   ifsese if_region = NULL;
1465   htab_t newivs_index, params_index;
1466   cloog_prog_clast pc;
1467   CloogState *state;
1468
1469   state = cloog_state_malloc ();
1470   timevar_push (TV_GRAPHITE_CODE_GEN);
1471   gloog_error = false;
1472
1473   pc = scop_to_clast (scop, state);
1474
1475   if (dump_file && (dump_flags & TDF_DETAILS))
1476     {
1477       fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1478       print_clast_stmt (dump_file, pc.stmt);
1479       fprintf (dump_file, "\n");
1480     }
1481
1482   recompute_all_dominators ();
1483   graphite_verify ();
1484
1485   if_region = move_sese_in_condition (region);
1486   sese_insert_phis_for_liveouts (region,
1487                                  if_region->region->exit->src,
1488                                  if_region->false_region->exit,
1489                                  if_region->true_region->exit);
1490   recompute_all_dominators ();
1491   graphite_verify ();
1492
1493   context_loop = SESE_ENTRY (region)->src->loop_father;
1494   newivs_index = htab_create (10, clast_name_index_elt_info,
1495                               eq_clast_name_indexes, free);
1496   params_index = htab_create (10, clast_name_index_elt_info,
1497                               eq_clast_name_indexes, free);
1498
1499   create_params_index (params_index, pc.prog);
1500
1501   translate_clast (region, context_loop, pc.stmt,
1502                    if_region->true_region->entry,
1503                    &newivs, newivs_index,
1504                    bb_pbb_mapping, 1, params_index);
1505   graphite_verify ();
1506   scev_reset_htab ();
1507   recompute_all_dominators ();
1508   graphite_verify ();
1509
1510   if (gloog_error)
1511     set_ifsese_condition (if_region, integer_zero_node);
1512
1513   free (if_region->true_region);
1514   free (if_region->region);
1515   free (if_region);
1516
1517   htab_delete (newivs_index);
1518   htab_delete (params_index);
1519   VEC_free (tree, heap, newivs);
1520   cloog_clast_free (pc.stmt);
1521   cloog_program_free (pc.prog);
1522   timevar_pop (TV_GRAPHITE_CODE_GEN);
1523
1524   if (dump_file && (dump_flags & TDF_DETAILS))
1525     {
1526       loop_p loop;
1527       loop_iterator li;
1528       int num_no_dependency = 0;
1529
1530       FOR_EACH_LOOP (li, loop, 0)
1531         if (loop->can_be_parallel)
1532           num_no_dependency++;
1533
1534       fprintf (dump_file, "\n%d loops carried no dependency.\n",
1535                num_no_dependency);
1536     }
1537
1538   cloog_state_free (state);
1539
1540   return !gloog_error;
1541 }
1542 #endif