OSDN Git Service

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