OSDN Git Service

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