OSDN Git Service

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